获取上个月最后一天、第一天

    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse("2019-03-15");
            Calendar c = Calendar.getInstance();
            //设置为指定日期
            c.setTime(date);
            //指定日期月份减去一
            c.add(Calendar.MONTH, -1);
            //指定日期月份减去一后的 最大天数
            c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
            //获取最终的时间
            Date lastDateOfPrevMonth = c.getTime();
            System.out.println("上月最后一天:" + format.format(lastDateOfPrevMonth));
            c.set(Calendar.DATE, 1);
            //获取最初的时间
            Date firstDateOfPrevMonth = c.getTime();
            System.out.println("上月最后第一天:" + format.format(firstDateOfPrevMonth));
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
    }
        //输出结果:
        //上月最后一天:2019-02-28
        //上月最后第一天:2019-02-01

文章已完
作者心情:昨夜西风凋碧树,独上高楼,望尽天涯路。
如无特殊说明,文章均为本站原创,转载请注明出处