//一年内的周一 public final class DateUtil{ /** *获得当前周的周一 的00:00:00 */ public static Date getMonDayOfCurrentWeak() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** *获得当前周的周天的23:59:59 */ public static Date getSunDayOfCurrentWeak(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.WEEK_OF_YEAR, 1); calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); }
改进版:跨年也没问题(依赖commons-lang.jar)
public final class DateUtil{ public static Date getMonDayOfCurrentWeak() { Calendar calendar = Calendar.getInstance(); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); Date date=null; if(dayOfWeek>Calendar.MONDAY){ date = DateUtils.addDays(calendar.getTime(), Calendar.MONDAY-dayOfWeek); calendar.setTime(date); }else if (dayOfWeek==Calendar.SUNDAY) { date = DateUtils.addDays(calendar.getTime(), -6); calendar.setTime(date); } calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } public static Date getSunDayOfCurrentWeak(){ Calendar calendar = Calendar.getInstance(); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); Date date=null; if(dayOfWeek>Calendar.SUNDAY){ date = DateUtils.addDays(calendar.getTime(), 8-dayOfWeek); calendar.setTime(date); } calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); } }
xxx