版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/65441549
public class Schedule {
private static Integer[] days = { 0, 1, 7, 6, 5, 4, 3, 2 };
public static void main(String[] args) {
getWeek(new Date());
}
public static Date getWeek(Date current) {
Integer currentWeek = DateUtil.getWeekDayNumber(current);
Integer day = days[currentWeek];
return DateUtil.getDateByDays(current, day);
}
}
public static Date getDateByDays(Date date, int days){
if(date == null) return null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DAY_OF_MONTH, days);
return calendar.getTime();
}
public static Integer getWeekDayNumber(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_WEEK);
}