Java 将一段时间以周、月、季分割
1 public class MyUtils { 2 /** 3 * 以季度分割时间段 4 * 此处季度是以 12-2月 3-5月 6-8月 9-11月 划分 5 * @param startTime 开始时间戳(毫秒) 6 * @param endTime 结束时间戳(毫秒) 7 * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应 8 * @param endDateList 结束段时间戳 和 开始段时间戳 一一对应 9 */ 10 public static void getIntervalTimeByQuarter(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) { 11 Date startDate = new Date(startTime); 12 Date endDate = new Date(endTime); 13 Calendar calendar = Calendar.getInstance(); 14 calendar.setTime(startDate); 15 int month = calendar.get(Calendar.MONTH) + 1; 16 switch (month) { 17 case 12: 18 case 3: 19 case 6: 20 case 9: 21 addTime(beginDateList, endDateList, startDate, endDate, calendar, 3); 22 break; 23 case 1: 24 case 4: 25 case 7: 26 case 10: 27 addTime(beginDateList, endDateList, startDate, endDate, calendar, 2); 28 break; 29 case 2: 30 case 5: 31 case 8: 32 case 11: 33 addTime(beginDateList, endDateList, startDate, endDate, calendar, 1); 34 break; 35 } 36 } 37 38 private static void addTime(List<Long> beginDateList, List<Long> endDateList, Date startDate, Date endDate, Calendar calendar, int i) { 39 beginDateList.add(startDate.getTime()); 40 calendar.add(Calendar.MONTH, i); 41 calendar.set(Calendar.DAY_OF_MONTH, 1); 42 calendar.add(Calendar.DATE, -1); 43 calendar.set(Calendar.HOUR_OF_DAY, 13); 44 calendar.set(Calendar.MINUTE, 59); 45 calendar.set(Calendar.SECOND, 59); 46 if (calendar.getTimeInMillis() > endDate.getTime()) { 47 endDateList.add(endDate.getTime()); 48 49 } else { 50 endDateList.add(calendar.getTimeInMillis()); 51 while (calendar.getTimeInMillis() < endDate.getTime()) { 52 calendar.add(Calendar.DATE, 1); 53 calendar.set(Calendar.HOUR_OF_DAY, 0); 54 calendar.set(Calendar.MINUTE, 0); 55 calendar.set(Calendar.SECOND, 0); 56 beginDateList.add(calendar.getTimeInMillis()); 57 calendar.add(Calendar.MONTH, 3); 58 calendar.set(Calendar.DAY_OF_MONTH, 1); 59 calendar.add(Calendar.DATE, -1); 60 calendar.set(Calendar.HOUR_OF_DAY, 13); 61 calendar.set(Calendar.MINUTE, 59); 62 calendar.set(Calendar.SECOND, 59); 63 if (calendar.getTimeInMillis() < endDate.getTime()) { 64 endDateList.add(calendar.getTimeInMillis()); 65 } else { 66 endDateList.add(endDate.getTime()); 67 } 68 } 69 } 70 } 71 72 /** 73 * 以周分割时间段 74 * 75 * @param startTime 开始时间戳(毫秒) 76 * @param endTime 结束时间戳(毫秒) 77 * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应 78 * @param endDateList 结束段时间戳 和 开始段时间戳 一一对应 79 */ 80 public static void getIntervalTimeByWeek(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) { 81 Date startDate = new Date(startTime); 82 Date endDate = new Date(endTime); 83 SimpleDateFormat sdw = new SimpleDateFormat("E"); 84 Calendar calendar = Calendar.getInstance(); 85 String begin = sdw.format(startDate); 86 calendar.setTime(startDate); 87 beginDateList.add(calendar.getTimeInMillis()); 88 if ("星期一".equals(begin)) { 89 addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar); 90 } else { 91 if ("星期日".equals(sdw.format(startDate))) { 92 Calendar special = Calendar.getInstance(); 93 special.setTime(startDate); 94 special.set(Calendar.HOUR_OF_DAY, 23); 95 special.set(Calendar.MINUTE, 59); 96 special.set(Calendar.SECOND, 59); 97 endDateList.add(special.getTime().getTime()); 98 } 99 addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar); 100 } 101 } 102 103 private static void addTimeStamp(List<Long> beginDateList, List<Long> endDateList, Date startDate, Date endDate, SimpleDateFormat sdw, Calendar calendar) { 104 while (startDate.getTime() < endDate.getTime()) { 105 calendar.add(Calendar.DAY_OF_YEAR, 1); 106 startDate = calendar.getTime(); 107 if ("星期一".equals(sdw.format(startDate))) { 108 calendar.set(Calendar.HOUR_OF_DAY, 0); 109 calendar.set(Calendar.MINUTE, 0); 110 calendar.set(Calendar.SECOND, 0); 111 beginDateList.add(calendar.getTimeInMillis()); 112 } else if ("星期日".equals(sdw.format(startDate)) || startDate.getTime() >= endDate.getTime()) { 113 if (startDate.getTime() <= endDate.getTime()) { 114 calendar.set(Calendar.HOUR_OF_DAY, 23); 115 calendar.set(Calendar.MINUTE, 59); 116 calendar.set(Calendar.SECOND, 59); 117 endDateList.add(calendar.getTimeInMillis()); 118 } else { 119 endDateList.add(endDate.getTime()); 120 } 121 } 122 } 123 } 124 125 /** 126 * 按照月份分割一段时间 127 * 128 * @param startTime 开始时间戳(毫秒) 129 * @param endTime 结束时间戳(毫秒) 130 * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应 131 * @param endDateList 结束段时间戳 和 开始段时间戳 一一对应 132 */ 133 public static void getIntervalTimeByMonth(Long startTime, Long endTime, List<Long> beginDateList, List<Long> endDateList) { 134 Date startDate = new Date(startTime); 135 Date endDate = new Date(endTime); 136 Calendar calendar = Calendar.getInstance(); 137 calendar.setTime(startDate); 138 beginDateList.add(calendar.getTimeInMillis()); 139 while (calendar.getTimeInMillis() < endDate.getTime()) { 140 calendar.add(Calendar.MONTH, 1); 141 calendar.set(Calendar.DAY_OF_MONTH, 1); 142 calendar.add(Calendar.DATE, -1); 143 calendar.set(Calendar.HOUR_OF_DAY, 13); 144 calendar.set(Calendar.MINUTE, 59); 145 calendar.set(Calendar.SECOND, 59); 146 if(calendar.getTimeInMillis() < endDate.getTime()){ 147 endDateList.add(calendar.getTimeInMillis()); 148 } else { 149 endDateList.add(endDate.getTime()); 150 break; 151 } 152 calendar.add(Calendar.DATE, 1); 153 calendar.set(Calendar.HOUR_OF_DAY, 0); 154 calendar.set(Calendar.MINUTE, 0); 155 calendar.set(Calendar.SECOND, 0); 156 beginDateList.add(calendar.getTimeInMillis()); 157 } 158 } 159 }