Calendar calendar=Calendar.getInstance();
if (TIME_UNIT_WEEK.equals(timeUnit)) {
calendar.setWeekDate(date/100,date%100,Calendar.MONDAY);
calendar.add(Calendar.WEEK_OF_YEAR,-3);
calendar.add(Calendar.DATE,5);
setTrendDate(calendar.get(Calendar.YEAR)*100 + calendar.get(Calendar.WEEK_OF_YEAR));
} else if (TIME_UNIT_MONTH.equals(timeUnit)) {
calendar.set(date / 100, date % 100 - 1, 1);
calendar.add(Calendar.MONTH, -3);
setTrendDate(calendar.get(Calendar.YEAR) * 100 + calendar.get(Calendar.MONTH)+1);
}
计算当前周/月前四周的日期
比如 date=201911 (2019年11月),则计算出为201908
201946(2019年46周),则计算出为201943
public void computeBeginAndEndDate() {
Assert.notNull(timeUnit, "timeUnit must not be null!");
Assert.notNull(date, "date must not be null!");
Calendar calendar = Calendar.getInstance();
if (TIME_UNIT_WEEK.equals(timeUnit)) {
calendar.setWeekDate(date / 100, date % 100, Calendar.MONDAY);
setBeginDate(calendar.getTime());
calendar.add(Calendar.DATE, 6);
setEndDate(calendar.getTime());
} else if (TIME_UNIT_MONTH.equals(timeUnit)) {
calendar.set(date / 100, date % 100 - 1, 1);
setBeginDate(calendar.getTime());
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setEndDate(calendar.getTime());
}
}
计算当前周/月的初始日期和结束日期
列:date=201911 (2019年11月),月初为20191101.月末为20191130
列:201946(2019年46周),初为周一的日期,末为礼拜天的日期