last day of month calculation
Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
This returns actual maximum for current month. For example it is February of leap year now, so it returns 29 as int
.
java.time.temporal.TemporalAdjusters.lastDayOfMonth()
Using the java.time
library built into Java 8, you can use the TemporalAdjuster
interface. We find an implementation ready for use in the TemporalAdjusters
utility class: lastDayOfMonth
.
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
LocalDate now = LocalDate.now(); //2015-11-23
LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth()); //2015-11-30
If you need to add time information, you may use any available LocalDate
to LocalDateTime
conversion like
lastDay.atStartOfDay(); //2015-11-30T00:00