获取时间是一年中的第几周。比如2019年12月31日是2019年的第53周,2020年的第一周。
public static Map<String, Integer> getWeekOfYear(String date) throws ParseException {
Map<String, Integer> result = new HashMap<>();
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
int week = cal.get(Calendar.WEEK_OF_YEAR);
if (week == 1 && cal.get(Calendar.MONTH) == 11) {
cal.add(Calendar.DATE, -6);
week = cal.get(Calendar.WEEK_OF_YEAR) + 1;
}
result.put("year", cal.get(Calendar.YEAR));
result.put("week", week);
return result;
}
网友评论