获取本月的第一天和最后一天
LocalDate localDate = LocalDate.now();
//本月第一天
LocalDate firstday = LocalDate.of(localDate.getYear(), localDate.getMonthValue(), 1);
//本月的最后一天
LocalDate lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth());
获取上个月的第一天和最后一天
LocalDate localDate = LocalDate.now();
if(localDate.getMonthValue() == 1){
LocalDate lastMonth = LocalDate.of(localDate.getYear() -1, 12, 1);
LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
} else {
LocalDate lastMonth = LocalDate.of(localDate.getYear(), localDate.getMonthValue() - 1, 1);
LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
}
或者使用Calendar :
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 0);
String lastMonthDay = sdf.format(calendar.getTime()); //上个月的最后一天