How can I get x
which should be 31 days before current_date
?
x(date)___________________________current_date
31
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With