Calendar设置周一为一周的第一天
1.获取过去一周的calendar对象
public static Calendar getLastWeekCalendar(){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
// 坑,设置为一周的第一天后,必须重新set时间后才会生效
calendar.setTime(new Date());
calendar.add(Calendar.DATE,-7);
// 设置当前时间为当前周的星期一
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return calendar;
}
2.特殊设置,遇到跨年的时候,一定要把中间间隔的日期算到上一周去
public static Integer getWeekOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int mouth = calendar.get(Calendar.MONTH);
// JDK think 2015-12-31 as 2016 1th week
//如果月份是12月,且求出来的周数是第一周,说明该日期实质上是这一年的第53周,也是下一年的第一周
if (mouth >= 11 && week <= 1) {
week += 52;
}
return week;
}
原创:做时间的朋友