Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the date of 31 days ago?

Tags:

java

jodatime

How can I get x which should be 31 days before current_date?

x(date)___________________________current_date
                       31 
like image 732
Nodirbek Shamsiev Avatar asked Jun 19 '15 07:06

Nodirbek Shamsiev


People also ask

How do you calculate number of days to date?

How do I go about calculating the days between two dates? To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years.


1 Answers

Just subtract 31 days. For example:

LocalDate current = new LocalDate(2015, 6, 19);
LocalDate x = current.minusDays(31); // 2015-05-19

To get the current date, you could use:

LocalDate current = new LocalDate(); // Default time zone

or

LocalDate current = new LocalDate(zone); // Some specific zone

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:

LocalDate current = clock.getCurrentInstant().toDateTime(zone).toLocalDate();

(That lets you use dependency injection to write simpler unit tests with a fake clock.)

like image 62
Jon Skeet Avatar answered Oct 03 '22 15:10

Jon Skeet