How do I calculate how many days are left in the month we are in, in Java?
For example, today is the 5th of November, the result would be:
25 days left for the end of the month.
How to do this?
How do I calculate how many days are left in the month we are in, in Java?
For example, today is the 5th of November, the result would be:
25 days left for the end of the month.
How to do this?
In Java there's been for a while Java Time API, which allows to get the number of days in the given month and calculate the difference using java.time.temporal.ChronoUnit.DAYS
:
LocalDate today = LocalDate.now();
LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());
long daysBetween = DAYS.between(today, endOfMonth);