Bootstrap

计算当前日期是一年中第几周

这个方法是计算给出日期是阴历年份的第几周,例如2020-01-05是农历2019年的最后一天,及输出2019-52,表示2019年第52周

public class MouthCount {
    public static void main(String[] args) throws ParseException {
        String datetime = "2020-01-06";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat formatMon = new SimpleDateFormat("MM");

        Date date = format.parse(datetime);

        int year = Integer.valueOf(formatYear.format(date));
        int month = Integer.valueOf(formatMon.format(date));

        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        int num = c.get(Calendar.WEEK_OF_YEAR);
        if (month == 1 && (num == 52||num == 53)) {
            System.out.println(year - 1 + "-" + (num < 10 ? "0" + num : num));
        } else {
            System.out.println(year + "-" + (num < 10 ? "0" + num : num));

        }
    }
}

欢迎转载使用!

;