[Java] 获取本月周次和日期时间段信息
package com.wdcloud.monitoring.common; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import com.wdcloud.monitoring.Model.WeekInfoModel; public class DateUtils { //是否打印日志 private static boolean isLog = false; /** * 获取本月周次和日期时间段信息 * * @return */ public static List<WeekInfoModel> getThisMonthWeekDate() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List<WeekInfoModel> list = new ArrayList<WeekInfoModel>(); // 1 获取本月的第一天 Date firstDayOfMonth = getFirstDayOfMonth(); Calendar firstDayOfMonthCal = Calendar.getInstance(); firstDayOfMonthCal.setFirstDayOfWeek(Calendar.MONDAY); firstDayOfMonthCal.setTime(firstDayOfMonth); // 2 获取本月第一周第一天 Date firstWeekFirstDay = getFirstDayOfFirstWeekOfMonth(); Calendar firstWeekFirstDayCal = Calendar.getInstance(); firstWeekFirstDayCal.setFirstDayOfWeek(Calendar.MONDAY); firstWeekFirstDayCal.setTime(firstWeekFirstDay); // 3 获取本次周期起止时间 Date sDate = null;// 本月第一周第一天 Date eDate = null;// 本月最后一周最后一天 Calendar sCal = Calendar.getInstance(); Calendar eCal = Calendar.getInstance(); sCal.setFirstDayOfWeek(Calendar.MONDAY); sCal.setFirstDayOfWeek(Calendar.MONDAY); // 获取本月第一天是周几 int weekDay = firstDayOfMonthCal.get(Calendar.DAY_OF_WEEK) - 1; // 如果是周末,起始时间就是本月第一周第一天 // 否则起始时间就是本月第一周第一天 倒退7天 if (weekDay == 6 || weekDay == 0 || weekDay == 1) {// 周六或周天 sDate = firstWeekFirstDay; } else { firstWeekFirstDayCal.getTime(); firstWeekFirstDayCal.add(Calendar.DATE, -7); sDate = firstWeekFirstDayCal.getTime(); } sCal.setTime(sDate); // 结束时间是起始时间+34天(5周) eCal.setTime(sDate); eDate = eCal.getTime(); eCal.add(Calendar.DATE, 34); eDate = eCal.getTime(); Date cDate = new Date(); if (cDate.getTime() < eDate.getTime()) { eDate = cDate; } if (isLog) { System.out.println(" 本月统计起止时间是:" + format.format(sDate) + " 到 " + format.format(eDate)); System.out .println("-------------------------------------------------------------------------------"); } // 4 循环得到周次信息 for (int i = 0; i < 5; i++) {// 循环5周次 WeekInfoModel model = new WeekInfoModel(); Calendar endTime = Calendar.getInstance(); endTime.setFirstDayOfWeek(Calendar.MONDAY); endTime.setTime(sDate); endTime.set(Calendar.HOUR_OF_DAY, 23); endTime.set(Calendar.MINUTE, 59); endTime.set(Calendar.SECOND, 59); endTime.add(Calendar.DATE, 6); model.setIndexOfWeek(i + 1); model.setStartDateOfWeek(sDate); model.setEndDateOfWeek(endTime.getTime()); Date currDate = new Date(); // 如果当前时间在某个周次内 if (sDate.getTime() < currDate.getTime() && endTime.getTime().getTime() > currDate.getTime()) { model.setEndDateOfWeek(currDate); if (isLog) { System.out.println(" 本月第" + (i + 1) + "周起止时间是:" + format.format(sDate) + " 到 " + format.format(currDate)); } list.add(model); break; } if (isLog) { System.out.println(" 本月第" + (i + 1) + "周起止时间是:" + format.format(sDate) + " 到 " + format.format(endTime.getTime())); } list.add(model); sCal.add(Calendar.DATE, 7); sDate = sCal.getTime(); } return list; } // 获取本月第一天 public static Date getFirstDayOfMonth() { Calendar now = Calendar.getInstance(); // Date date = new Date(2015 - 1900, 12 - 1, 1); // now.setTime(date); now.set(Calendar.DATE, now.getActualMinimum(Calendar.DATE)); now.set(Calendar.HOUR_OF_DAY, 0); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } // 获取本月第一周第一天 public static Date getFirstDayOfFirstWeekOfMonth() { Calendar now = Calendar.getInstance(); now.setFirstDayOfWeek(Calendar.MONDAY); now.setTime(getFirstDayOfMonth()); int i = 1; while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { now.set(Calendar.DAY_OF_MONTH, i++);// 设置这个月的星期1 为几号 } Date firstMonday = now.getTime();// 取得日期和时间 String dtStr = new SimpleDateFormat("YYYY-MM-dd").format(firstMonday);// 格式化日期 if (isLog) { System.out .println("-------------------------------------------------------------------------------"); System.out.println(" 本月第一周第一天是:" + dtStr); System.out .println("-------------------------------------------------------------------------------"); } return now.getTime(); } /** * 获取本周周次和日期时间段信息 * * @return */ public static WeekInfoModel getThisWeekDate() { WeekInfoModel model = new WeekInfoModel(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 根据今天的时间获取本周属于本月的第几周 Calendar now = Calendar.getInstance(); now.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期 int wom = now.get(Calendar.WEEK_OF_MONTH); model.setIndexOfWeek(wom); // 根据今天的时间获取本周的开始时间 now.set(Calendar.HOUR_OF_DAY, 0); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); now.set(Calendar.MILLISECOND, 0); System.out.println("********" + now.get(Calendar.YEAR) + "年" + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期一的日期是:" + df.format(now.getTime())); model.setStartDateOfWeek(now.getTime()); // 根据今天的时间获取本周的结束时间 now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); now.add(Calendar.WEEK_OF_YEAR, 1); now.set(Calendar.HOUR_OF_DAY, 23); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); now.set(Calendar.MILLISECOND, 999); System.out.println("********" + now.get(Calendar.YEAR) + "年" + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期天的日期是:" + df.format(now.getTime())); model.setEndDateOfWeek(now.getTime()); return model; } }