1.使用LocalDate方法获取
LocalDate now = LocalDate.now();
now = now.minusMonths(1);//月份-1
// 获取当前月的第一天
LocalDate firstDay = now.with(TemporalAdjusters.firstDayOfMonth());
// 获取当前月的最后一天
LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
//上个月第一天
Date startTime = Date.from(firstDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
//上个月最后一天
Date endTime = Date.from(lastDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
int year = now.getYear();
int month = now.getMonthValue();
2.使用Date方法获取
Calendar cal=Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date startTime = cal.getTime();//上个月第一天
Calendar calendar=Calendar.getInstance();
int month=calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date endTime = calendar.getTime();//上个月最后一天