DateUtils.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. package com.zhongzheng.common.utils;
  2. import cn.hutool.core.lang.Validator;
  3. import org.apache.commons.lang3.RandomStringUtils;
  4. import org.apache.commons.lang3.time.DateFormatUtils;
  5. import java.lang.management.ManagementFactory;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.time.*;
  9. import java.time.format.DateTimeFormatter;
  10. import java.util.*;
  11. /**
  12. * 时间工具类
  13. *
  14. * @author zhongzheng
  15. */
  16. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  17. {
  18. public static String YYYY = "yyyy";
  19. public static String YYYY_MM = "yyyy-MM";
  20. public static String YYYY_MM_DD = "yyyy-MM-dd";
  21. public static String YYYYMMDD = "yyyyMMdd";
  22. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  23. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  24. private static String[] parsePatterns = {
  25. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  26. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  27. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  28. /**
  29. * 获取当前Date型日期
  30. *
  31. * @return Date() 当前日期
  32. */
  33. public static Date getNowDate()
  34. {
  35. return new Date();
  36. }
  37. /**
  38. * 获取当前日期, 默认格式为yyyy-MM-dd
  39. *
  40. * @return String
  41. */
  42. public static String getDate()
  43. {
  44. return dateTimeNow(YYYY_MM_DD);
  45. }
  46. public static final String getTime()
  47. {
  48. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  49. }
  50. public static final String dateTimeNow()
  51. {
  52. return dateTimeNow(YYYYMMDDHHMMSS);
  53. }
  54. public static final String dateTimeNow(final String format)
  55. {
  56. return parseDateToStr(format, new Date());
  57. }
  58. public static final String dateTime(final Date date)
  59. {
  60. return parseDateToStr(YYYY_MM_DD, date);
  61. }
  62. public static final String parseDateToStr(final String format, final Date date)
  63. {
  64. return new SimpleDateFormat(format).format(date);
  65. }
  66. public static final Date dateTimeThrow(final String format, final String ts) throws ParseException {
  67. return new SimpleDateFormat(format).parse(ts);
  68. }
  69. public static final Date dateTime(final String format, final String ts)
  70. {
  71. try
  72. {
  73. return new SimpleDateFormat(format).parse(ts);
  74. }
  75. catch (ParseException e)
  76. {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. public static final Long dateTimeSec(final String format, final String ts)
  81. {
  82. try
  83. {
  84. return (new SimpleDateFormat(format).parse(ts)).getTime()/1000;
  85. }
  86. catch (ParseException e)
  87. {
  88. throw new RuntimeException(e);
  89. }
  90. }
  91. public static String timestampToDateFormat(Long times){
  92. if(Validator.isEmpty(times)){
  93. return "";
  94. }
  95. long t = times.longValue();
  96. t = t * 1000;
  97. Date date = new Date(t);
  98. return DateFormatUtils.format(date, "yyyy/MM/dd");
  99. }
  100. public static Date timeToDate(Long times){
  101. long t = times.longValue();
  102. t = t * 1000;
  103. return new Date(t);
  104. }
  105. public static String timestampToDateFormatMonth(Long times){
  106. if(Validator.isEmpty(times)){
  107. return "";
  108. }
  109. long t = times.longValue();
  110. t = t * 1000;
  111. Date date = new Date(t);
  112. return DateFormatUtils.format(date, "MM月dd号");
  113. }
  114. public static String timestampToDateFormatMonthTwo(Long times){
  115. if(Validator.isEmpty(times)){
  116. return "";
  117. }
  118. long t = times.longValue();
  119. t = t * 1000;
  120. Date date = new Date(t);
  121. return DateFormatUtils.format(date, "MM.dd");
  122. }
  123. public static String timestampToDateFormat(Long times,String patternStr){
  124. if(Validator.isEmpty(times)){
  125. return "";
  126. }
  127. long t = times.longValue();
  128. t = t * 1000;
  129. Date date = new Date(t);
  130. return DateFormatUtils.format(date, patternStr);
  131. }
  132. /**
  133. * 将秒转为时分秒格式【01:01:01】
  134. * @param second 需要转化的秒数
  135. * @return
  136. */
  137. public static String secondConvertHourMinSecond(Long second) {
  138. String str = "";
  139. if (second == null || second < 0) {
  140. return str;
  141. }
  142. // 得到小时
  143. long h = second / 3600;
  144. str = h > 0 ? ((h < 10 ? ("0" + h) : h) + "时") : "";
  145. // 得到分钟
  146. long m = (second % 3600) / 60;
  147. str += m > 0? (m < 10 ? ("0" + m) : m) + "分":"";
  148. //得到剩余秒
  149. long s = second % 60;
  150. str += s > 0?(s < 10 ? ("0" + s) : s)+"秒":"";
  151. return str;
  152. }
  153. public static String timestampToDate(Long times){
  154. if(Validator.isEmpty(times)){
  155. return "";
  156. }
  157. long t = times.longValue();
  158. t = t * 1000;
  159. Date date = new Date(t);
  160. return dateTime(date);
  161. }
  162. /**
  163. * 日期路径 即年/月/日 如2018/08/08
  164. */
  165. public static final String datePath()
  166. {
  167. Date now = new Date();
  168. return DateFormatUtils.format(now, "yyyy/MM/dd");
  169. }
  170. /**
  171. * 日期路径 即年/月/日 如20180808
  172. */
  173. public static final String dateTime()
  174. {
  175. Date now = new Date();
  176. return DateFormatUtils.format(now, "yyyyMMdd");
  177. }
  178. /**
  179. * 日期型字符串转化为日期 格式
  180. */
  181. public static Date parseDate(Object str)
  182. {
  183. if (str == null)
  184. {
  185. return null;
  186. }
  187. try
  188. {
  189. return parseDate(str.toString(), parsePatterns);
  190. }
  191. catch (ParseException e)
  192. {
  193. return null;
  194. }
  195. }
  196. /**
  197. * 获取服务器启动时间
  198. */
  199. public static Date getServerStartDate()
  200. {
  201. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  202. return new Date(time);
  203. }
  204. /**
  205. * 计算两个时间差
  206. */
  207. public static String getDatePoor(Date endDate, Date nowDate)
  208. {
  209. long nd = 1000 * 24 * 60 * 60;
  210. long nh = 1000 * 60 * 60;
  211. long nm = 1000 * 60;
  212. // long ns = 1000;
  213. // 获得两个时间的毫秒时间差异
  214. long diff = endDate.getTime() - nowDate.getTime();
  215. // 计算差多少天
  216. long day = diff / nd;
  217. // 计算差多少小时
  218. long hour = diff % nd / nh;
  219. // 计算差多少分钟
  220. long min = diff % nd % nh / nm;
  221. // 计算差多少秒//输出结果
  222. // long sec = diff % nd % nh % nm / ns;
  223. return day + "天" + hour + "小时" + min + "分钟";
  224. }
  225. public static Long getNowTime()
  226. {
  227. return System.currentTimeMillis()/1000;
  228. }
  229. /**
  230. * 获取今天凌晨时间戳
  231. */
  232. public static Long getTodayZeroTime()
  233. {
  234. Calendar cal = Calendar.getInstance();
  235. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  236. return cal.getTimeInMillis() / 1000; //今天凌晨
  237. }
  238. /**
  239. * 获取指定时间的凌晨时间戳
  240. */
  241. public static Long getScheduleTimeStrZeroTime(String timeStr,String patternStr)
  242. {
  243. Long time = dateTimeSec(patternStr,timeStr);
  244. Calendar cal = Calendar.getInstance();
  245. cal.setTime(timeToDate(time));
  246. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  247. return cal.getTimeInMillis() / 1000; //今天凌晨
  248. }
  249. /**
  250. * 获取指定时间的凌晨时间戳
  251. */
  252. public static Long getScheduleTimeZeroTime(Long scheduleTime)
  253. {
  254. Calendar cal = Calendar.getInstance();
  255. cal.setTime(timeToDate(scheduleTime));
  256. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  257. return cal.getTimeInMillis() / 1000; //今天凌晨
  258. }
  259. public static String formatDate(Date time,String str)
  260. {
  261. SimpleDateFormat sdf = new SimpleDateFormat(str);
  262. return sdf.format(time);
  263. }
  264. /**
  265. * 获取明天凌晨时间戳
  266. */
  267. public static Long getTomorrowZeroTime()
  268. {
  269. Calendar cal = Calendar.getInstance();
  270. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  271. Calendar cal1 = Calendar.getInstance();
  272. cal1.setTime(cal.getTime());
  273. cal1.add(Calendar.DAY_OF_MONTH , +1);
  274. return cal1.getTimeInMillis() / 1000; //明天凌晨
  275. }
  276. /**
  277. * 获取当月第一天的凌晨时间戳
  278. */
  279. public static Long getToMonthZeroTime()
  280. {
  281. Calendar cal = Calendar.getInstance();
  282. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
  283. return cal.getTime().getTime()/1000;
  284. }
  285. /**
  286. * 获取本年第一天的凌晨时间戳
  287. */
  288. public static Long getToYearZeroTime()
  289. {
  290. Calendar calendar = Calendar.getInstance();
  291. // 设置当前时间为年初的第一天
  292. calendar.set(Calendar.DAY_OF_YEAR, 1);
  293. Date startOfCurrentYear = calendar.getTime();
  294. return startOfCurrentYear.getTime()/1000;
  295. }
  296. /**
  297. * 根据当前日期获得所在周的日期区间(周一和周日日期)
  298. */
  299. public static Map<String, Long> getTimeInterval(Date date){
  300. Map<String, Long> map = new HashMap<>();
  301. Calendar cal = Calendar.getInstance();
  302. cal.setTime(date);
  303. // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
  304. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
  305. if(1 == dayWeek){
  306. cal.add(Calendar.DAY_OF_MONTH,-1);
  307. }
  308. // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  309. cal.setFirstDayOfWeek(Calendar.MONDAY);
  310. // 获得当前日期是一个星期的第几天
  311. int day = cal.get(Calendar.DAY_OF_WEEK);
  312. // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
  313. cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
  314. Long imptimeBegin = cal.getTime().getTime();
  315. cal.add(Calendar.DATE,6);
  316. Long imptimeEnd = cal.getTime().getTime();
  317. map.put("start", imptimeBegin/1000);
  318. map.put("end", imptimeEnd/1000);
  319. return map;
  320. }
  321. /**
  322. * 根据当前日期获得上周的日期区间(上周周一和周日日期)
  323. */
  324. public static Map<String, Long> getLastTimeInterval(Date date){
  325. Map<String, Long> map = new HashMap<>();
  326. Calendar calendar1 = Calendar.getInstance();
  327. Calendar calendar2 = Calendar.getInstance();
  328. calendar1.setTime(date);
  329. calendar2.setTime(date);
  330. int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
  331. if(dayOfWeek <= 0){
  332. dayOfWeek = 7;
  333. }
  334. int offset1 = 1 - dayOfWeek;
  335. int offset2 = 7 - dayOfWeek;
  336. calendar1.add(Calendar.DATE, offset1 - 7);
  337. calendar2.add(Calendar.DATE, offset2 - 7);
  338. // last Monday
  339. Long lastBeginDate = calendar1.getTime().getTime();
  340. // last Sunday
  341. Long lastEndDate = calendar2.getTime().getTime();
  342. map.put("laststart", lastBeginDate/1000);
  343. map.put("lastend", lastEndDate/1000);
  344. return map;
  345. }
  346. /**
  347. * 获取日期格式订单号
  348. * @return
  349. */
  350. public static String getDateOrderSn()
  351. {
  352. DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  353. String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
  354. //随机数
  355. String randomNumeric = RandomStringUtils.randomNumeric(8);
  356. return localDate+randomNumeric;
  357. }
  358. public static String getDateInputOrderSn()
  359. {
  360. return "LD"+getDateOrderSn();
  361. }
  362. public static String getPayOrderSn()
  363. {
  364. return "P"+getDateOrderSn();
  365. }
  366. public static String getInvoiceOrderSn()
  367. {
  368. DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  369. String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
  370. //随机数
  371. String randomNumeric = RandomStringUtils.randomNumeric(4);
  372. return "IN"+localDate+randomNumeric;
  373. }
  374. public static String getTagOrderSn(String tag)
  375. {
  376. return tag+getDateOrderSn();
  377. }
  378. public static String secToTime(int time) {
  379. String timeStr = null;
  380. int hour = 0;
  381. int minute = 0;
  382. int second = 0;
  383. if (time <= 0)
  384. return "00:00";
  385. else {
  386. minute = time / 60;
  387. if (minute < 60) {
  388. second = time % 60;
  389. timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
  390. } else {
  391. hour = minute / 60;
  392. if (hour > 99){
  393. return "99:59:59";
  394. }
  395. minute = minute % 60;
  396. second = time - hour * 3600 - minute * 60;
  397. timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
  398. }
  399. }
  400. return timeStr;
  401. }
  402. public static String unitFormat(int i) {
  403. String retStr = null;
  404. if (i >= 0 && i < 10)
  405. retStr = "0" + Integer.toString(i);
  406. else
  407. retStr = "" + i;
  408. return retStr;
  409. }
  410. public static Integer durationFormat(String duration) {
  411. int index1=duration.indexOf(":");
  412. int index2=duration.indexOf(":",index1+1);
  413. int hh=Integer.parseInt(duration.substring(0,index1));
  414. int mi=Integer.parseInt(duration.substring(index1+1,index2));
  415. int ss=Integer.parseInt(duration.substring(index2+1));
  416. return hh*60*60+mi*60+ss;
  417. }
  418. public static Integer dayBetween(Long s1,Long s2) {
  419. String date1str = timestampToDateFormat(s1);
  420. String date2str = timestampToDateFormat(s2);
  421. int count = 0;
  422. if("".equals(date1str) || date1str == null || "".equals(date2str) || date2str == null) {
  423. return count;
  424. }
  425. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  426. try {
  427. Date date1 = format.parse(date1str);
  428. Date date2 = format.parse(date2str);
  429. count = ((int) ((date1.getTime() - date2.getTime()) / (1000*3600*24)));
  430. return count;
  431. }catch (Exception e){
  432. return null;
  433. }
  434. }
  435. public static Integer getTodayWeek(){
  436. Calendar calendar = Calendar.getInstance();
  437. calendar.setTime(new Date());
  438. int weekIdx = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  439. switch (weekIdx) {
  440. case 1:
  441. return 1;
  442. case 2:
  443. return 2;
  444. case 3:
  445. return 3;
  446. case 4:
  447. return 4;
  448. case 5:
  449. return 5;
  450. case 6:
  451. return 6;
  452. default:
  453. return 7;
  454. }
  455. }
  456. public static LocalDate[] getDateArray() {
  457. // 创建一个长度为30的数组
  458. LocalDate[] dates = new LocalDate[30];
  459. // 获取今天的日期
  460. LocalDate today = LocalDate.now();
  461. // 用循环给数组赋值
  462. for (int i = 0; i < dates.length; i++) {
  463. // 用today.plusDays(i)得到第i天的日期
  464. dates[i] = today.plusDays(i);
  465. }
  466. // 返回数组
  467. return dates;
  468. }
  469. static List<String> holiday =new ArrayList<>();
  470. static List<String> extraWorkDay =new ArrayList<>();
  471. public static Boolean isWorkingDay(long time) {
  472. LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.of("+8"));
  473. String formatTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  474. initHoliday();
  475. initExtraWorkDay();
  476. //是否加班日
  477. if(extraWorkDay.contains(formatTime)){
  478. return true;
  479. }
  480. //是否节假日
  481. if(holiday.contains(formatTime)){
  482. return false;
  483. }
  484. //如果是1-5表示周一到周五 是工作日
  485. DayOfWeek week = dateTime.getDayOfWeek();
  486. if(week==DayOfWeek.SATURDAY||week==DayOfWeek.SUNDAY){
  487. return false;
  488. }
  489. return true;
  490. }
  491. /**
  492. * 初始化节假日
  493. */
  494. public static void initHoliday(){
  495. holiday.add("2023-06-22");
  496. holiday.add("2023-06-23");
  497. holiday.add("2023-09-29");
  498. holiday.add("2023-09-30");
  499. holiday.add("2023-10-01");
  500. holiday.add("2023-10-02");
  501. holiday.add("2023-10-03");
  502. holiday.add("2023-10-04");
  503. holiday.add("2023-10-05");
  504. holiday.add("2023-10-06");
  505. }
  506. /**
  507. * 初始化额外加班日
  508. */
  509. public static void initExtraWorkDay(){
  510. extraWorkDay.add("2023-06-25");
  511. extraWorkDay.add("2023-10-07");
  512. extraWorkDay.add("2023-10-08");
  513. }
  514. public static Long getAppointTime(Long millisecond, Integer day) {
  515. for (Integer i = 0; i < day; i++) {
  516. Long dayAfter = getDayBefore(millisecond, 1);
  517. Calendar calendar = Calendar.getInstance();
  518. calendar.setTime(timeToDate(dayAfter));
  519. int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  520. String[] weeks = new String[]{"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  521. if (weeks[index].equals("星期六") || weeks[index].equals("星期天")) {
  522. day += 1;
  523. }
  524. //判断当前是否为工作日
  525. if (!isWorkingDay(dayAfter)) {
  526. day += 1;
  527. }
  528. millisecond = dayAfter;
  529. }
  530. return millisecond;
  531. }
  532. /**
  533. * 指定时间往前或往后推n天
  534. *
  535. * @param dateTime 指定时间
  536. * @param x 指定天数
  537. * @return
  538. */
  539. public static Long getDayBefore(Long dateTime, int x) {
  540. Calendar c = Calendar.getInstance();
  541. Date date = new Date(dateTime*1000);
  542. c.setTime(date);
  543. int day = c.get(Calendar.DATE);
  544. c.set(Calendar.DATE, day - x); //往前推几天
  545. //c.set(Calendar.DATE, day + x); 往后推几天
  546. return c.getTime().getTime()/1000;
  547. }
  548. /**
  549. * 指定时间往前或往后推n天
  550. *
  551. * @param dateTime 指定时间
  552. * @param x 指定天数
  553. * @return
  554. */
  555. public static Long getDayAfter(Long dateTime, int x) {
  556. Calendar c = Calendar.getInstance();
  557. Date date = new Date(dateTime*1000);
  558. c.setTime(date);
  559. int day = c.get(Calendar.DATE);
  560. // c.set(Calendar.DATE, day - x); //往前推几天
  561. c.set(Calendar.DATE, day + x); //往后推几天
  562. return c.getTime().getTime()/1000;
  563. }
  564. public static List<Long> getWeekData(Long dataTime){
  565. List<Long> week = new ArrayList();
  566. Calendar calendar = Calendar.getInstance();
  567. Long zeroTime = DateUtils.getScheduleTimeZeroTime(dataTime);
  568. calendar.setTime(timeToDate(zeroTime));
  569. // 如果是周日
  570. if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  571. calendar.add(Calendar.DAY_OF_YEAR,-1);
  572. }
  573. // 获取当前日期是当周的第i天
  574. int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  575. // 获取当前日期所在周的第一天
  576. calendar.add(Calendar.DATE , -i);
  577. for (int j = 0; j < 7; j++) {
  578. if(j >0){
  579. calendar.add(Calendar.DATE , 1);
  580. }
  581. Long time = calendar.getTime().getTime()/1000;
  582. if (j == 6){
  583. time = time + 86400L;
  584. }
  585. week.add(time);
  586. }
  587. return week;
  588. }
  589. public static Map<Long,Long> getWeekTime(Long startTime,Long endTime){
  590. Map<Long,Long> mapList = new LinkedHashMap<>();
  591. Long zeroTime = DateUtils.getScheduleTimeZeroTime(startTime);
  592. //一天的时间戳
  593. Long time = 86400L;
  594. for (Long i = zeroTime; i <= endTime; i = i+time) {
  595. List<Long> weekData = DateUtils.getWeekData(i);
  596. mapList.put(weekData.get(0),weekData.get(weekData.size()-1));
  597. }
  598. return mapList;
  599. }
  600. public static Map<Long,Long> getMonthTime(Long startTime,Long endTime){
  601. //一天的时间戳
  602. Long time = 86400L;
  603. Map<Long,Long> map = new LinkedHashMap<>();
  604. try{
  605. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
  606. Date d1 = DateUtils.timeToDate(startTime);
  607. Date d2 = DateUtils.timeToDate(endTime);
  608. Calendar dd = Calendar.getInstance();//定义日期实例
  609. dd.setTime(d1);//设置日期起始时间
  610. while (dd.getTime().before(d2)) {//判断是否到结束日期
  611. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  612. String str = sdf.format(dd.getTime());
  613. Calendar c = Calendar.getInstance();
  614. c.setTime(format.parse(str));
  615. c.add(Calendar.MONTH, 0);
  616. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  617. Long start = c.getTime().getTime()/1000;
  618. dd.setTime(DateUtils.timeToDate(start));
  619. //获取当前月最后一天
  620. Calendar ca = Calendar.getInstance();
  621. ca.setTime(format.parse(str));
  622. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  623. Long end = ca.getTime().getTime()/1000 + time;
  624. map.put(start,end);
  625. dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
  626. }
  627. }catch (Exception e){
  628. System.out.println("异常"+e.getMessage());
  629. }
  630. return map;
  631. }
  632. }