java - Date earlier than a month ago -


how check whether given date earlier month ago? fastest algorithm? have take account different months have different numbers of days.

updated java 8

the class localdate class can used:

localdate adate = localdate.parse("2017-01-01"); return adate.isbefore( localdate.now().minusmonths(1)); 

for previous versions, calendar class work.

calendar calendar = calendar.getinstance(); calendar.add( calendar.month ,  -1 ); return adate.compareto( calendar.gettime() ) < 0; 

sample code:

import static java.lang.system.out; import java.time.localdate;  public class sample {     public static void main( string [] args ) {         localdate amonthago = localdate.now().minusmonths(1);         out.println( localdate.parse("2009-12-16").isbefore(amonthago));         out.println( localdate.now().isbefore(amonthago));         out.println( localdate.parse("2017-12-24").isbefore(amonthago));     } } 

prints

true false false 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -