How can I get x
which should be 31 days before current_date
?
x(date)___________________________current_date
31
You can try this:
LocalDate current = new LocalDate();//Constructs an instance set to the current local time evaluated using ISO chronology in the default zone.
LocalDate x = current.minusDays(31);
Or otherwise you can try:
LocalDate current = LocalDate.now();//Obtains a LocalDate set to the current system millisecond time using ISOChronology in the default time zone
LocalDate x = current.minusDays(31);
You can used JODA API if you want, its very advance and useful features:
String DATE_PATTERN = "dd/MM/yyyy";
DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_PATTERN);
String systemDate = formatter.print(DateTime.now());
System.out.println("Current Date : " + systemDate);
String newDate = formatter.print(DateTime.now().minusDays(31));
System.out.println("Date 31 days ago : " + newDate);
Output: Current Date : 19/06/2015
Date 31 days ago : 19/05/2015
Just subtract 31 days. For example:
To get the current date, you could use:
or
Or you may want to create your own "clock" representation which is able to give you the current
Instant
, in which case you'd use:(That lets you use dependency injection to write simpler unit tests with a fake clock.)