本文所有使用的時(shí)間戳均為毫秒級(jí) 得到的集合區(qū)間也是毫秒級(jí)的時(shí)間戳
-
按周分割時(shí)間
- 代碼如下:
@Test public void timeSplitByWeek() { // 1.開始時(shí)間 2019-06-09 13:16:04 Long startTime = 1560057364000L; // 2.結(jié)束時(shí)間 2019-07-09 13:16:04 Long endTime = 1562649364000L; // 3.開始時(shí)間段區(qū)間集合 List<Long> beginDateList = new ArrayList<Long>(); // 4.結(jié)束時(shí)間段區(qū)間集合 List<Long> endDateList = new ArrayList<Long>(); // 5.調(diào)用工具類 MyUtils.getIntervalTimeByWeek(startTime, endTime, beginDateList, endDateList); // 6.打印輸出 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (int i = 0; i < beginDateList.size(); i++) { Long beginStr = beginDateList.get(i); Long endStr = endDateList.get(i); String begin1 = sdf.format(new Date(beginStr)); String end1 = sdf.format(new Date(endStr)); System.out.println("第" + i + "段時(shí)間區(qū)間:" + begin1 + "-------" + end1); } }
-
運(yùn)行結(jié)果如下圖所示:
- 代碼如下:
-
按月分割時(shí)間
- 代碼如下:
@Test public void timeSplitByMonth() { // 1.開始時(shí)間 2017-02-03 13:16:04 Long startTime = 1486098964000L; // 2.結(jié)束時(shí)間 2019-07-03 13:16:05 Long endTime = 1562130965000L; // 3.開始時(shí)間段區(qū)間集合 List<Long> beginDateList = new ArrayList<Long>(); // 4.結(jié)束時(shí)間段區(qū)間集合 List<Long> endDateList = new ArrayList<Long>(); // 5.調(diào)用工具類 MyUtils.getIntervalTimeByMonth(startTime, endTime, beginDateList, endDateList); // 6.打印輸出 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (int i = 0; i < beginDateList.size(); i++) { Long beginStr = beginDateList.get(i); Long endStr = endDateList.get(i); String begin1 = sdf.format(new Date(beginStr)); String end1 = sdf.format(new Date(endStr)); System.out.println("第" + i + "段時(shí)間區(qū)間:" + begin1 + "-------" + end1); } }
-
運(yùn)行結(jié)果如下圖所示:
- 代碼如下:
-
按季分割時(shí)間
- 代碼如下:
@Test public void timeSplitByQuarter() { // 1.開始時(shí)間 2018-12-09 13:16:04 Long startTime = 1544332564000L; // 2.結(jié)束時(shí)間 2019-12-09 13:16:04 Long endTime = 1575868564000L; // 3.開始時(shí)間段區(qū)間集合 List<Long> beginDateList = new ArrayList<Long>(); // 4.結(jié)束時(shí)間段區(qū)間集合 List<Long> endDateList = new ArrayList<Long>(); // 5.調(diào)用工具類 MyUtils.getIntervalTimeByQuarter(startTime, endTime, beginDateList, endDateList); // 6.打印輸出 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (int i = 0; i < beginDateList.size(); i++) { Long beginStr = beginDateList.get(i); Long endStr = endDateList.get(i); String begin1 = sdf.format(new Date(beginStr)); String end1 = sdf.format(new Date(endStr)); System.out.println("第" + i + "段時(shí)間區(qū)間:" + begin1 + "-------" + end1); } }
-
運(yùn)行結(jié)果如下圖所示:
- 代碼如下:
-
工具類MyUtils代碼如下所示:
public class MyUtils { /** * 以季度分割時(shí)間段 * 此處季度是以 12-2月 3-5月 6-8月 9-11月 劃分 * @param startTime 開始時(shí)間戳(毫秒) * @param endTime 結(jié)束時(shí)間戳(毫秒) * @param beginDateList 開始段時(shí)間戳 和 結(jié)束段時(shí)間戳 一一對(duì)應(yīng) * @param endDateList 結(jié)束段時(shí)間戳 和 開始段時(shí)間戳 一一對(duì)應(yīng) */ public static void getIntervalTimeByQuarter(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) { Date startDate = new Date(startTime); Date endDate = new Date(endTime); Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); int month = calendar.get(Calendar.MONTH) + 1; switch (month) { case 12: case 3: case 6: case 9: addTime(beginDateList, endDateList, startDate, endDate, calendar, 3); break; case 1: case 4: case 7: case 10: addTime(beginDateList, endDateList, startDate, endDate, calendar, 2); break; case 2: case 5: case 8: case 11: addTime(beginDateList, endDateList, startDate, endDate, calendar, 1); break; } } private static void addTime(List<Long> beginDateList, List<Long> endDateList, Date startDate, Date endDate, Calendar calendar, int i) { beginDateList.add(startDate.getTime()); calendar.add(Calendar.MONTH, i); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); calendar.set(Calendar.HOUR_OF_DAY, 13); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); if (calendar.getTimeInMillis() > endDate.getTime()) { endDateList.add(endDate.getTime()); } else { endDateList.add(calendar.getTimeInMillis()); while (calendar.getTimeInMillis() < endDate.getTime()) { calendar.add(Calendar.DATE, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); beginDateList.add(calendar.getTimeInMillis()); calendar.add(Calendar.MONTH, 3); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); calendar.set(Calendar.HOUR_OF_DAY, 13); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); if (calendar.getTimeInMillis() < endDate.getTime()) { endDateList.add(calendar.getTimeInMillis()); } else { endDateList.add(endDate.getTime()); } } } } /** * 以周分割時(shí)間段 * * @param startTime 開始時(shí)間戳(毫秒) * @param endTime 結(jié)束時(shí)間戳(毫秒) * @param beginDateList 開始段時(shí)間戳 和 結(jié)束段時(shí)間戳 一一對(duì)應(yīng) * @param endDateList 結(jié)束段時(shí)間戳 和 開始段時(shí)間戳 一一對(duì)應(yīng) */ public static void getIntervalTimeByWeek(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) { Date startDate = new Date(startTime); Date endDate = new Date(endTime); SimpleDateFormat sdw = new SimpleDateFormat("E"); Calendar calendar = Calendar.getInstance(); String begin = sdw.format(startDate); calendar.setTime(startDate); beginDateList.add(calendar.getTimeInMillis()); if ("星期一".equals(begin)) { addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar); } else { if ("星期日".equals(sdw.format(startDate))) { Calendar special = Calendar.getInstance(); special.setTime(startDate); special.set(Calendar.HOUR_OF_DAY, 23); special.set(Calendar.MINUTE, 59); special.set(Calendar.SECOND, 59); endDateList.add(special.getTime().getTime()); } addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar); } } private static void addTimeStamp(List<Long> beginDateList, List<Long> endDateList, Date startDate, Date endDate, SimpleDateFormat sdw, Calendar calendar) { while (startDate.getTime() < endDate.getTime()) { calendar.add(Calendar.DAY_OF_YEAR, 1); startDate = calendar.getTime(); if ("星期一".equals(sdw.format(startDate))) { calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); beginDateList.add(calendar.getTimeInMillis()); } else if ("星期日".equals(sdw.format(startDate)) || startDate.getTime() >= endDate.getTime()) { if (startDate.getTime() <= endDate.getTime()) { calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); endDateList.add(calendar.getTimeInMillis()); } else { endDateList.add(endDate.getTime()); } } } } /** * 按照月份分割一段時(shí)間 * * @param startTime 開始時(shí)間戳(毫秒) * @param endTime 結(jié)束時(shí)間戳(毫秒) * @param beginDateList 開始段時(shí)間戳 和 結(jié)束段時(shí)間戳 一一對(duì)應(yīng) * @param endDateList 結(jié)束段時(shí)間戳 和 開始段時(shí)間戳 一一對(duì)應(yīng) */ public static void getIntervalTimeByMonth(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) { Date startDate = new Date(startTime); Date endDate = new Date(endTime); Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); beginDateList.add(calendar.getTimeInMillis()); while (calendar.getTimeInMillis() < endDate.getTime()) { calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); calendar.set(Calendar.HOUR_OF_DAY, 13); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); if(calendar.getTimeInMillis() < endDate.getTime()){ endDateList.add(calendar.getTimeInMillis()); } else { endDateList.add(endDate.getTime()); break; } calendar.add(Calendar.DATE, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); beginDateList.add(calendar.getTimeInMillis()); } } }