DatesUtil.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package com.huaxu.util;
  2. import java.sql.Timestamp;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.time.Instant;
  6. import java.time.LocalDate;
  7. import java.time.LocalDateTime;
  8. import java.time.ZoneId;
  9. import java.time.format.DateTimeFormatter;
  10. import java.time.temporal.ChronoUnit;
  11. import java.util.*;
  12. /*
  13. * 由于为了以后使用方便,所有方法的返回类型都设为了 java.utils.Date 请在使用时根据自己的需要进行日期格式化处理,如:
  14. *
  15. * import java.text.SimpleDateFormat;SimpleDateFormat simpleDateFormat = new
  16. * SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String todayBegin =
  17. * simpleDateFormat.format
  18. * (DateUtils.getDayBegin());System.out.println(todayBegin );//输出结果为2017-10-26
  19. * 00:00:00
  20. */
  21. /**
  22. * 日期工具类
  23. */
  24. public class DatesUtil {
  25. public static final String DATE_TIME_FORMAT="yyyy-MM-dd HH:mm:ss";
  26. public static final String DATE_FORMAT = "yyyy-MM-dd";
  27. // 获取当天的开始时间
  28. public static Date getDayBegin() {
  29. Calendar cal = new GregorianCalendar();
  30. cal.set(Calendar.HOUR_OF_DAY, 0);
  31. cal.set(Calendar.MINUTE, 0);
  32. cal.set(Calendar.SECOND, 0);
  33. cal.set(Calendar.MILLISECOND, 0);
  34. return cal.getTime();
  35. }
  36. // 获取当天的结束时间
  37. public static Date getDayEnd() {
  38. Calendar cal = new GregorianCalendar();
  39. cal.set(Calendar.HOUR_OF_DAY, 23);
  40. cal.set(Calendar.MINUTE, 59);
  41. cal.set(Calendar.SECOND, 59);
  42. return cal.getTime();
  43. }
  44. // 获取昨天的开始时间
  45. public static Date getBeginDayOfYesterday() {
  46. Calendar cal = new GregorianCalendar();
  47. cal.setTime(getDayBegin());
  48. cal.add(Calendar.DAY_OF_MONTH, -1);
  49. return cal.getTime();
  50. }
  51. // 获取昨天的结束时间
  52. public static Date getEndDayOfYesterDay() {
  53. Calendar cal = new GregorianCalendar();
  54. cal.setTime(getDayEnd());
  55. cal.add(Calendar.DAY_OF_MONTH, -1);
  56. return cal.getTime();
  57. }
  58. // 获取明天的开始时间
  59. public static Date getBeginDayOfTomorrow() {
  60. Calendar cal = new GregorianCalendar();
  61. cal.setTime(getDayBegin());
  62. cal.add(Calendar.DAY_OF_MONTH, 1);
  63. return cal.getTime();
  64. }
  65. // 获取明天的结束时间
  66. public static Date getEndDayOfTomorrow() {
  67. Calendar cal = new GregorianCalendar();
  68. cal.setTime(getDayEnd());
  69. cal.add(Calendar.DAY_OF_MONTH, 1);
  70. return cal.getTime();
  71. }
  72. // 获取本周的开始时间
  73. @SuppressWarnings("unused")
  74. public static Date getBeginDayOfWeek() {
  75. Date date = new Date();
  76. if (date == null) {
  77. return null;
  78. }
  79. Calendar cal = Calendar.getInstance();
  80. cal.setTime(date);
  81. int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
  82. if (dayofweek == 1) {
  83. dayofweek += 7;
  84. }
  85. cal.add(Calendar.DATE, 2 - dayofweek);
  86. return getDayStartTime(cal.getTime());
  87. }
  88. // 获取本周的结束时间
  89. public static Date getEndDayOfWeek() {
  90. Calendar cal = Calendar.getInstance();
  91. cal.setTime(getBeginDayOfWeek());
  92. cal.add(Calendar.DAY_OF_WEEK, 6);
  93. Date weekEndSta = cal.getTime();
  94. return getDayEndTime(weekEndSta);
  95. }
  96. // 获取上周的开始时间
  97. @SuppressWarnings("unused")
  98. public static Date getBeginDayOfLastWeek() {
  99. Date date = new Date();
  100. if (date == null) {
  101. return null;
  102. }
  103. Calendar cal = Calendar.getInstance();
  104. cal.setTime(date);
  105. int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
  106. if (dayofweek == 1) {
  107. dayofweek += 7;
  108. }
  109. cal.add(Calendar.DATE, 2 - dayofweek - 7);
  110. return getDayStartTime(cal.getTime());
  111. }
  112. // 获取上周的结束时间
  113. public static Date getEndDayOfLastWeek() {
  114. Calendar cal = Calendar.getInstance();
  115. cal.setTime(getBeginDayOfLastWeek());
  116. cal.add(Calendar.DAY_OF_WEEK, 6);
  117. Date weekEndSta = cal.getTime();
  118. return getDayEndTime(weekEndSta);
  119. }
  120. // 获取本月的开始时间
  121. public static Date getBeginDayOfMonth() {
  122. Calendar calendar = Calendar.getInstance();
  123. calendar.set(getNowYear(), getNowMonth() - 1, 1);
  124. return getDayStartTime(calendar.getTime());
  125. }
  126. // 获取本月的结束时间
  127. public static Date getEndDayOfMonth() {
  128. Calendar calendar = Calendar.getInstance();
  129. calendar.set(getNowYear(), getNowMonth() - 1, 1);
  130. int day = calendar.getActualMaximum(5);
  131. calendar.set(getNowYear(), getNowMonth() - 1, day);
  132. return getDayEndTime(calendar.getTime());
  133. }
  134. // 获取上月的开始时间
  135. public static Date getBeginDayOfLastMonth() {
  136. Calendar calendar = Calendar.getInstance();
  137. calendar.set(getNowYear(), getNowMonth() - 2, 1);
  138. return getDayStartTime(calendar.getTime());
  139. }
  140. // 获取上月的结束时间
  141. public static Date getEndDayOfLastMonth() {
  142. Calendar calendar = Calendar.getInstance();
  143. calendar.set(getNowYear(), getNowMonth() - 2, 1);
  144. int day = calendar.getActualMaximum(5);
  145. calendar.set(getNowYear(), getNowMonth() - 2, day);
  146. return getDayEndTime(calendar.getTime());
  147. }
  148. // 获取本年的开始时间
  149. public static Date getBeginDayOfYear() {
  150. Calendar cal = Calendar.getInstance();
  151. cal.set(Calendar.YEAR, getNowYear());
  152. cal.set(Calendar.MONTH, Calendar.JANUARY);
  153. cal.set(Calendar.DATE, 1);
  154. return getDayStartTime(cal.getTime());
  155. }
  156. // 获取本年的结束时间
  157. public static Date getEndDayOfYear() {
  158. Calendar cal = Calendar.getInstance();
  159. cal.set(Calendar.YEAR, getNowYear());
  160. cal.set(Calendar.MONTH, Calendar.DECEMBER);
  161. cal.set(Calendar.DATE, 31);
  162. return getDayEndTime(cal.getTime());
  163. }
  164. // 获取某个日期的开始时间
  165. public static Timestamp getDayStartTime(Date d) {
  166. Calendar calendar = Calendar.getInstance();
  167. if (null != d) {
  168. calendar.setTime(d);
  169. }
  170. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
  171. calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  172. calendar.set(Calendar.MILLISECOND, 0);
  173. return new Timestamp(calendar.getTimeInMillis());
  174. }
  175. // 获取某个日期的结束时间
  176. public static Timestamp getDayEndTime(Date d) {
  177. Calendar calendar = Calendar.getInstance();
  178. if (null != d) {
  179. calendar.setTime(d);
  180. }
  181. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
  182. calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
  183. calendar.set(Calendar.MILLISECOND, 999);
  184. return new Timestamp(calendar.getTimeInMillis());
  185. }
  186. // 获取今年是哪一年
  187. public static Integer getNowYear() {
  188. Date date = new Date();
  189. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  190. gc.setTime(date);
  191. return Integer.valueOf(gc.get(1));
  192. }
  193. // 获取本月是哪一月
  194. public static int getNowMonth() {
  195. Date date = new Date();
  196. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  197. gc.setTime(date);
  198. return gc.get(2) + 1;
  199. }
  200. // 两个日期相减得到的天数
  201. public static int getDiffDays(Date beginDate, Date endDate) {
  202. if (beginDate == null || endDate == null) {
  203. throw new IllegalArgumentException("getDiffDays param is null!");
  204. }
  205. long diff = (endDate.getTime() - beginDate.getTime())
  206. / (1000 * 60 * 60 * 24);
  207. int days = new Long(diff).intValue();
  208. return days;
  209. }
  210. // 两个日期相减得到的毫秒数
  211. public static long dateDiff(Date beginDate, Date endDate) {
  212. long date1ms = beginDate.getTime();
  213. long date2ms = endDate.getTime();
  214. return date2ms - date1ms;
  215. }
  216. // 获取两个日期中的最大日期
  217. public static Date max(Date beginDate, Date endDate) {
  218. if (beginDate == null) {
  219. return endDate;
  220. }
  221. if (endDate == null) {
  222. return beginDate;
  223. }
  224. if (beginDate.after(endDate)) {
  225. return beginDate;
  226. }
  227. return endDate;
  228. }
  229. // 获取两个日期中的最小日期
  230. public static Date min(Date beginDate, Date endDate) {
  231. if (beginDate == null) {
  232. return endDate;
  233. }
  234. if (endDate == null) {
  235. return beginDate;
  236. }
  237. if (beginDate.after(endDate)) {
  238. return endDate;
  239. }
  240. return beginDate;
  241. }
  242. // 返回某月该季度的第一个月
  243. public static Date getFirstSeasonDate(Date date) {
  244. final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
  245. Calendar cal = Calendar.getInstance();
  246. cal.setTime(date);
  247. int sean = SEASON[cal.get(Calendar.MONTH)];
  248. cal.set(Calendar.MONTH, sean * 3 - 3);
  249. return cal.getTime();
  250. }
  251. // 返回某个日期下几天的日期
  252. public static Date getNextDay(Date date, int i) {
  253. Calendar cal = new GregorianCalendar();
  254. cal.setTime(date);
  255. cal.set(Calendar.HOUR_OF_DAY, 0);
  256. cal.set(Calendar.MINUTE, 0);
  257. cal.set(Calendar.SECOND, 0);
  258. cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
  259. return cal.getTime();
  260. }
  261. public static Date getNextMonth(Date date ,int i){
  262. Calendar cal = new GregorianCalendar();
  263. cal.setTime(date);
  264. cal.set(Calendar.HOUR_OF_DAY, 0);
  265. cal.set(Calendar.MINUTE, 0);
  266. cal.set(Calendar.SECOND, 0);
  267. cal.add(Calendar.MONTH, 0);
  268. cal.set(Calendar.DAY_OF_MONTH, 1);
  269. cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + i);
  270. return cal.getTime();
  271. }
  272. // 返回某个日期前几天的日期
  273. public static Date getFrontDay(Date date, int i) {
  274. Calendar cal = new GregorianCalendar();
  275. cal.setTime(date);
  276. cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
  277. return cal.getTime();
  278. }
  279. // 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
  280. @SuppressWarnings({ "rawtypes", "unchecked" })
  281. public static List getTimeList(int beginYear, int beginMonth, int endYear,
  282. int endMonth, int k) {
  283. List list = new ArrayList();
  284. if (beginYear == endYear) {
  285. for (int j = beginMonth; j <= endMonth; j++) {
  286. list.add(getTimeList(beginYear, j, k));
  287. }
  288. } else {
  289. {
  290. for (int j = beginMonth; j < 12; j++) {
  291. list.add(getTimeList(beginYear, j, k));
  292. }
  293. for (int i = beginYear + 1; i < endYear; i++) {
  294. for (int j = 0; j < 12; j++) {
  295. list.add(getTimeList(i, j, k));
  296. }
  297. }
  298. for (int j = 0; j <= endMonth; j++) {
  299. list.add(getTimeList(endYear, j, k));
  300. }
  301. }
  302. }
  303. return list;
  304. }
  305. // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
  306. @SuppressWarnings({ "unchecked", "rawtypes" })
  307. public static List getTimeList(int beginYear, int beginMonth, int k) {
  308. List list = new ArrayList();
  309. Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
  310. int max = begincal.getActualMaximum(Calendar.DATE);
  311. for (int i = 1; i < max; i = i + k) {
  312. list.add(begincal.getTime());
  313. begincal.add(Calendar.DATE, k);
  314. }
  315. begincal = new GregorianCalendar(beginYear, beginMonth, max);
  316. list.add(begincal.getTime());
  317. return list;
  318. }
  319. public static String formatNow(String format) {
  320. SimpleDateFormat sdf = new SimpleDateFormat(format);
  321. String formatStr = sdf.format(new Date());
  322. return formatStr;
  323. }
  324. public static String formatNow() {
  325. SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
  326. String formatStr = sdf.format(new Date());
  327. return formatStr;
  328. }
  329. public static String formatDate(Date date ,String format) {
  330. SimpleDateFormat sdf = new SimpleDateFormat(format);
  331. String formatStr = sdf.format(date);
  332. return formatStr;
  333. }
  334. // 当前日期的后几天
  335. public static Date afterNow(int day) {
  336. Calendar cal=Calendar.getInstance();
  337. cal.add(Calendar.DATE,Math.abs(day));
  338. Date time=cal.getTime();
  339. return time;
  340. }
  341. // 当前日期的前几天
  342. public static Date beforeNow(int day) {
  343. Calendar cal=Calendar.getInstance();
  344. cal.add(Calendar.DATE,-Math.abs(day));
  345. Date time=cal.getTime();
  346. return time;
  347. }
  348. public static Date before(Date date ,int n) {
  349. Calendar cal=Calendar.getInstance();
  350. cal.setTime(date);
  351. cal.add(Calendar.DATE,-n);
  352. return cal.getTime();
  353. }
  354. public static Date after(Date date,int n) {
  355. Calendar cal=Calendar.getInstance();
  356. cal.setTime(date);
  357. cal.add(Calendar.DATE,n);
  358. return cal.getTime();
  359. }
  360. public static List<String> beforeNowDateArray(int day){
  361. List<String> dates = new ArrayList<String>();
  362. Calendar cal=Calendar.getInstance();
  363. for(int i =1 ;i<=Math.abs(day);i++) {
  364. cal.add(Calendar.DATE,-1);
  365. dates.add(formatDate(cal.getTime(),"yyyyMMdd"));
  366. }
  367. return dates;
  368. }
  369. public static Set<String> betweenDate(Date startDate ,Date endDate){
  370. Set<String> dates = new HashSet<String>();
  371. Calendar cal=Calendar.getInstance();
  372. cal.setTime(endDate);
  373. while(endDate.after(startDate)) {
  374. dates.add(formatDate(cal.getTime(),"yyyyMMdd"));
  375. cal.add(Calendar.DATE,-1);
  376. cal.setTime(cal.getTime());
  377. endDate = cal.getTime();
  378. }
  379. dates.add(formatDate(startDate,"yyyyMMdd"));
  380. return dates;
  381. }
  382. public static Set<String> betweenMonth(Date startDate ,Date endDate){
  383. Set<String> dates = new HashSet<>();
  384. Calendar cal=Calendar.getInstance();
  385. cal.setTime(endDate);
  386. while(endDate.after(startDate)) {
  387. dates.add(formatDate(cal.getTime(),"yyyyMM"));
  388. cal.add(Calendar.MONTH,-1);
  389. cal.setTime(cal.getTime());
  390. endDate = cal.getTime();
  391. }
  392. dates.add(formatDate(startDate,"yyyyMM"));
  393. return dates;
  394. }
  395. public static Date parseDate(String dateStr,String format) {
  396. Date date = null ;
  397. SimpleDateFormat sdf = new SimpleDateFormat(format);
  398. try {
  399. date = sdf.parse(dateStr);
  400. } catch (ParseException e) {
  401. e.printStackTrace();
  402. }
  403. return date;
  404. }
  405. /**
  406. * 判断某一时间是否在一个区间内
  407. *
  408. * @param sourceTime
  409. * 时间区间,半闭合,如[10:00-20:00]
  410. * @param curTime
  411. * 需要判断的时间 如10:00
  412. * @return
  413. * @throws IllegalArgumentException
  414. */
  415. public static boolean isInTime(String sourceTime, String curTime) {
  416. if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
  417. throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
  418. }
  419. if (curTime == null || !curTime.contains(":")) {
  420. throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
  421. }
  422. String[] args = sourceTime.split("-");
  423. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  424. try {
  425. long now = sdf.parse(curTime).getTime();
  426. long start = sdf.parse(args[0]).getTime();
  427. long end = sdf.parse(args[1]).getTime();
  428. if (args[1].equals("00:00")) {
  429. args[1] = "24:00";
  430. }
  431. if (end < start) {
  432. if (now > end && now < start) {
  433. return false;
  434. } else {
  435. return true;
  436. }
  437. }
  438. else {
  439. if (now >= start && now <= end) {
  440. return true;
  441. } else {
  442. return false;
  443. }
  444. }
  445. } catch (ParseException e) {
  446. e.printStackTrace();
  447. throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
  448. }
  449. }
  450. public static Date before(Date d ,long milSeconds) {
  451. return new Date(d.getTime()-milSeconds);
  452. }
  453. public static Date yesterdayLastDate() {
  454. Calendar calendar = Calendar.getInstance();
  455. calendar.setTime(new Date());
  456. calendar.add(Calendar.DATE, -1);
  457. calendar.set(Calendar.HOUR_OF_DAY, 23);
  458. calendar.set(Calendar.MINUTE, 59);
  459. calendar.set(Calendar.SECOND, 59);
  460. calendar.set(Calendar.MILLISECOND, 999);
  461. return calendar.getTime();
  462. }
  463. public static Date dateToISODate(Date date) {
  464. //T代表后面跟着时间,Z代表UTC统一时间
  465. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  466. format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
  467. String isoDate = format.format(date);
  468. try {
  469. return format.parse(isoDate);
  470. } catch (ParseException e) {
  471. e.printStackTrace();
  472. }
  473. return null;
  474. }
  475. public static Date beforeMonth(int month){
  476. Date date = new Date();
  477. Calendar calendar = Calendar.getInstance();
  478. calendar.setTime(date); // 设置为当前时间
  479. calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
  480. date = calendar.getTime();
  481. return date ;
  482. }
  483. public static Date beforeYear(int year){
  484. Date date = new Date();
  485. Calendar calendar = Calendar.getInstance();
  486. calendar.setTime(date); // 设置为当前时间
  487. calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1); // 设置为上一年
  488. date = calendar.getTime();
  489. return date ;
  490. }
  491. // Wed Jan 02 03:09:49 CST 2019 -> 2019-02-02 03:09:49
  492. public static String format(String timeString){
  493. try {
  494. SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH) ;
  495. SimpleDateFormat sfEnd = new SimpleDateFormat(DATE_TIME_FORMAT);
  496. String format = sfEnd.format(sfStart.parse(timeString));
  497. return format ;
  498. }catch (Exception e){
  499. e.printStackTrace();
  500. }
  501. return null ;
  502. }
  503. /**
  504. * @Author lihui
  505. * @Description 获取月份具体天数
  506. * @Date 2021/4/15
  507. * @Param [timeString]
  508. * @return int
  509. **/
  510. public static int getMouthDays(String timeString) {
  511. SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM");
  512. Calendar rightNow = Calendar.getInstance();
  513. try{
  514. rightNow.setTime(simpleDate.parse(timeString));
  515. }catch(ParseException e){
  516. e.printStackTrace();
  517. }
  518. int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);//根据年月 获取月份天数
  519. return days;
  520. }
  521. public static boolean isInTime(String startTime, String endTime, String inTime, String pattern) {
  522. SimpleDateFormat simpleDate = new SimpleDateFormat(pattern);
  523. try {
  524. // 10:00 11:00 = > 10:15
  525. Date startDate = simpleDate.parse(startTime);
  526. Date endDate = simpleDate.parse(endTime);
  527. Date inDate = simpleDate.parse(inTime);
  528. return startDate.before(inDate) && endDate.after(inDate);
  529. } catch (ParseException e) {
  530. e.printStackTrace();
  531. }
  532. return false;
  533. }
  534. public static boolean isInTime(String startTime, String endTime, Date inTime, String pattern) {
  535. SimpleDateFormat simpleDate = new SimpleDateFormat(pattern);
  536. try {
  537. // 10:00 11:00 = > 10:15
  538. Date startDate = simpleDate.parse(startTime);
  539. Date endDate = simpleDate.parse(endTime);
  540. Date inDate = simpleDate.parse(simpleDate.format(inTime));
  541. return startDate.before(inDate) && endDate.after(inDate);
  542. } catch (ParseException e) {
  543. e.printStackTrace();
  544. }
  545. return false;
  546. }
  547. public static Date addDayOfDate(Date date,int i){
  548. Calendar c = Calendar.getInstance();
  549. c.setTime(date);
  550. c.add(Calendar.DATE, i);
  551. Date newDate = c.getTime();
  552. return newDate;
  553. }
  554. public static Date addMinuteOfDate(Date date,int i){
  555. Calendar c = Calendar.getInstance();
  556. c.setTime(date);
  557. c.add(Calendar.MINUTE, i);
  558. Date newDate = c.getTime();
  559. return newDate;
  560. }
  561. public static LocalDate parseLocalDate(Date date){
  562. Instant instant = date.toInstant();
  563. ZoneId zoneId = ZoneId.systemDefault();
  564. return instant.atZone(zoneId).toLocalDate();
  565. }
  566. public static LocalDate formatLocalDate(String date){
  567. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  568. return LocalDate.parse(date, fmt);
  569. }
  570. public static long minutesBetween(Date startTime, Date endTime){
  571. return ChronoUnit.MINUTES.between(Instant.ofEpochMilli(startTime.getTime()), Instant.ofEpochMilli(endTime.getTime()));
  572. }
  573. public static void main(String[] args) {
  574. //String d = "2019-11-14 13:10:16";
  575. //System.out.println(DatesUtil.parseDate(d, DatesUtil.DATE_TIME_FORMAT));
  576. // System.out.println(DatesUtil.formatDate(getNextDay(DatesUtil.beforeNow(7),1), DatesUtil.DATE_TIME_FORMAT));
  577. // System.out.println(DatesUtil.formatDate(getNextDay(DatesUtil.beforeMonth(1),1), DatesUtil.DATE_TIME_FORMAT));
  578. // Set<String> iter = DatesUtil.betweenDate(DatesUtil.beforeMonth(1), new Date());
  579. // System.out.println(DatesUtil.formatDate(getNextMonth(DatesUtil.beforeYear(1),1), DatesUtil.DATE_TIME_FORMAT));
  580. // Set<String> iter1 = DatesUtil.betweenMonth(DatesUtil.beforeYear(1),new Date());
  581. // System.out.println(JSON.toJSONString(iter));
  582. // System.out.println(JSON.toJSONString(iter1));
  583. System.out.println(DatesUtil.formatDate(DatesUtil.addDayOfDate(new Date(), -(3-1)),"yyyy-MM-dd") + " 00:00:00");
  584. }
  585. }