1 /**
 2      * 获取上个月的最后一天23点59分59秒的时间
 3      */
 4     private  String getBeforeLastMonthdate()throws Exception{
 5         SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 6         Calendar calendar=Calendar.getInstance();
 7         int month=calendar.get(Calendar.MONTH);
 8         calendar.set(Calendar.MONTH, month-1);
 9         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
10         //将小时至23
11         calendar.set(Calendar.HOUR_OF_DAY, 23);
12         //将分钟至59
13         calendar.set(Calendar.MINUTE, 59);
14         //将秒至59
15         calendar.set(Calendar.SECOND,59);
16         String format = sf.format(calendar.getTime());
17         return format;
18     }
1 /**
 2      * 获取上一个月1号0点0分0秒的时间
 3      */
 4     private  String getBeforeFirstMonthdate()throws Exception{
 5         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 6         Calendar calendar=Calendar.getInstance();
 7         calendar.add(Calendar.MONTH, -1);
 8         calendar.set(Calendar.DAY_OF_MONTH, 1);
 9         //将小时至23
10         calendar.set(Calendar.HOUR_OF_DAY, 23);
11         //将分钟至59
12         calendar.set(Calendar.MINUTE, 59);
13         //将秒至59
14         calendar.set(Calendar.SECOND,59);
15         String format1 = format.format(calendar.getTime());
16         return format1;
17     }

 

打印出来结果是:
上个月第一天:2022-06-01 00:00:00
上个月最后一天:2022-06-30 23:59:59