Java 获取当月天数、上月最大天数

今天正好是3月,可以测试特殊月份的2月

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); // 设置时区
        SimpleDateFormat oSdf = new SimpleDateFormat ("yyyy-MM-dd");
        String format = oSdf.format(new Date()); 
        cal.setTime(oSdf.parse(format)); // 设置时间
        int thisMonth = cal.get(Calendar.DAY_OF_MONTH); // 得到当月
        cal.add(Calendar.MONTH, -1); // 得到上月月份
        int lastMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 得到上月总天数
posted @ 2022-03-03 10:03  张三丰学Java  阅读(1223)  评论(0编辑  收藏  举报