DateUtils.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package com.ruixuan.common.utils;
  2. import org.apache.commons.lang3.time.DateFormatUtils;
  3. import java.lang.management.ManagementFactory;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.time.LocalDate;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. import java.time.ZoneId;
  11. import java.time.ZonedDateTime;
  12. import java.util.ArrayList;
  13. import java.util.Calendar;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * 时间工具类
  20. *
  21. * @author ruoyi
  22. */
  23. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  24. public static String YYYY = "yyyy";
  25. public static String YYYY_MM = "yyyy-MM";
  26. public static String YYYY_MM_DD = "yyyy-MM-dd";
  27. public static String YYYYMMDD = "yyyyMMdd";
  28. public static String YYMMDD = "yyMMdd";
  29. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  30. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  31. private static String[] parsePatterns = {
  32. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  33. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  34. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  35. /**
  36. * 获取当前Date型日期
  37. *
  38. * @return Date() 当前日期
  39. */
  40. public static Date getNowDate() {
  41. return new Date();
  42. }
  43. public static String getNowDate(String format) {
  44. SimpleDateFormat sdf = new SimpleDateFormat(format);
  45. Date date = new Date();
  46. return sdf.format(date);
  47. }
  48. public static String getNowDateStr() {
  49. SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");
  50. return simp.format(new Date());
  51. }
  52. /**
  53. * 获取当前日期, 默认格式为yyyy-MM-dd
  54. *
  55. * @return String
  56. */
  57. public static String getDate() {
  58. return dateTimeNow(YYYY_MM_DD);
  59. }
  60. public static final String getTime() {
  61. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  62. }
  63. public static final String dateTimeNow() {
  64. return dateTimeNow(YYYYMMDDHHMMSS);
  65. }
  66. public static final String dateTimeNow(final String format) {
  67. return parseDateToStr(format, new Date());
  68. }
  69. public static final String dateTime(final Date date) {
  70. return parseDateToStr(YYYY_MM_DD, date);
  71. }
  72. public static final String parseDateToStr(final String format, final Date date) {
  73. return new SimpleDateFormat(format).format(date);
  74. }
  75. public static final Date dateTime(final String format, final String ts) {
  76. try {
  77. return new SimpleDateFormat(format).parse(ts);
  78. } catch (ParseException e) {
  79. throw new RuntimeException(e);
  80. }
  81. }
  82. /**
  83. * 日期路径 即年/月/日 如2018/08/08
  84. */
  85. public static final String datePath() {
  86. Date now = new Date();
  87. return DateFormatUtils.format(now, "yyyy/MM/dd");
  88. }
  89. /**
  90. * 日期路径 即年/月/日 如20180808
  91. */
  92. public static final String dateTime() {
  93. Date now = new Date();
  94. return DateFormatUtils.format(now, "yyyyMMdd");
  95. }
  96. /**
  97. * 日期型字符串转化为日期 格式
  98. */
  99. public static Date parseDate(Object str) {
  100. if (str == null) {
  101. return null;
  102. }
  103. try {
  104. return parseDate(str.toString(), parsePatterns);
  105. } catch (ParseException e) {
  106. return null;
  107. }
  108. }
  109. /**
  110. * 获取服务器启动时间
  111. */
  112. public static Date getServerStartDate() {
  113. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  114. return new Date(time);
  115. }
  116. /**
  117. * 计算相差天数
  118. */
  119. public static int differentDaysByMillisecond(Date date1, Date date2) {
  120. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  121. }
  122. /**
  123. * 计算两个时间差
  124. */
  125. public static String getDatePoor(Date endDate, Date nowDate) {
  126. long nd = 1000 * 24 * 60 * 60;
  127. long nh = 1000 * 60 * 60;
  128. long nm = 1000 * 60;
  129. // long ns = 1000;
  130. // 获得两个时间的毫秒时间差异
  131. long diff = endDate.getTime() - nowDate.getTime();
  132. // 计算差多少天
  133. long day = diff / nd;
  134. // 计算差多少小时
  135. long hour = diff % nd / nh;
  136. // 计算差多少分钟
  137. long min = diff % nd % nh / nm;
  138. // 计算差多少秒//输出结果
  139. // long sec = diff % nd % nh % nm / ns;
  140. return day + "天" + hour + "小时" + min + "分钟";
  141. }
  142. /**
  143. * 增加 LocalDateTime ==> Date
  144. */
  145. public static Date toDate(LocalDateTime temporalAccessor) {
  146. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  147. return Date.from(zdt.toInstant());
  148. }
  149. /**
  150. * 增加 LocalDate ==> Date
  151. */
  152. public static Date toDate(LocalDate temporalAccessor) {
  153. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  154. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  155. return Date.from(zdt.toInstant());
  156. }
  157. /**
  158. * 时间字符串转long类型
  159. * 2022-05-01 ===>>>> 20220501
  160. *
  161. * @param date
  162. * @return
  163. */
  164. public static Long strDateToInt(String date) {
  165. return Long.valueOf(date.replaceAll("-", ""));
  166. }
  167. /**
  168. * 时间字符串转long类型
  169. * 2022-05-01 00:00:00 ===>>>> 20220501
  170. *
  171. * @param date
  172. * @return
  173. */
  174. public static Long strDateTimsToInt(String date) {
  175. String[] datas = date.split(" ");
  176. return Long.valueOf(datas[0].replaceAll("-", ""));
  177. }
  178. /**
  179. * @param
  180. * @description: 获取当前周的开始和截至时间
  181. * @return: java.util.Map
  182. * @author: zianY
  183. * @time: 2022/6/1
  184. */
  185. public static Map getWeekBeginAndEnd() {
  186. Map<String, String> timeMap = new HashMap<>();
  187. String begin = parseDateToStr(YYYY_MM_DD, getBeginDayOfWeek());
  188. String end = parseDateToStr(YYYY_MM_DD, getEendDayOfWeek());
  189. timeMap.put("begin", begin + " 00:00:00");
  190. timeMap.put("end", end + " 23:59:59");
  191. return timeMap;
  192. }
  193. /**
  194. * @param
  195. * @description: 获取本周开始时间
  196. * @return: java.util.Date
  197. * @author: zianY
  198. * @time: 2022/6/1
  199. */
  200. public static Date getBeginDayOfWeek() {
  201. Date date = new Date();
  202. Calendar cal = Calendar.getInstance();
  203. cal.setTime(date);
  204. int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
  205. if (dayOfWeek == 1) {
  206. dayOfWeek += 7;
  207. }
  208. cal.add(Calendar.DATE, 2 - dayOfWeek);
  209. return cal.getTime();
  210. }
  211. /**
  212. * @param
  213. * @description: 获取本周结束时间
  214. * @return: java.util.Date
  215. * @author: zianY
  216. * @time: 2022/6/1
  217. */
  218. public static Date getEendDayOfWeek() {
  219. Calendar cal = Calendar.getInstance();
  220. cal.setTime(getBeginDayOfWeek());
  221. cal.add(Calendar.DAY_OF_WEEK, 6);
  222. return cal.getTime();
  223. }
  224. /**
  225. * 指定模式的时间格式
  226. */
  227. private static SimpleDateFormat getSDFormat(String pattern) {
  228. return new SimpleDateFormat(pattern);
  229. }
  230. /**
  231. * 指定日期按指定格式显示
  232. */
  233. public static String formatDate(Date date, String pattern) {
  234. return getSDFormat(pattern).format(date);
  235. }
  236. /**
  237. * 日期转换为时间戳
  238. */
  239. public static long timeToStamp(String timers) {
  240. Date d = new Date();
  241. long timeStemp = 0;
  242. try {
  243. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  244. // 日期转换为时间戳
  245. d = sf.parse(timers);
  246. } catch (ParseException e) {
  247. e.printStackTrace();
  248. }
  249. timeStemp = d.getTime();
  250. return timeStemp;
  251. }
  252. /**
  253. * 时间戳转换为字符串
  254. *
  255. * @param time
  256. * @return
  257. */
  258. public static String timestamptoStr(Long timestamp) {
  259. Date date = new Date();
  260. date.setTime(timestamp);
  261. return DateFormatUtils.format(date, YYYYMMDD);
  262. }
  263. /**
  264. * 时间戳转换为小时(0-23)
  265. */
  266. public static Integer timestamptoHour(Long timestamp) {
  267. Date date = new Date();
  268. date.setTime(timestamp);
  269. String hh = DateFormatUtils.format(date, "HH");
  270. return Integer.valueOf(hh);
  271. }
  272. /**
  273. * 时间戳转换为字符串
  274. *
  275. * @param time
  276. * @return
  277. */
  278. public static String timestamptoMinutesAndSecondsStr(Long timestamp) {
  279. Date date = new Date();
  280. date.setTime(timestamp);
  281. return DateFormatUtils.format(date, YYYY_MM_DD_HH_MM_SS);
  282. }
  283. /**
  284. * 返回 日期-天数 的string 日期
  285. *
  286. * @param time 时间
  287. * @param daysBetween 天数
  288. * @return
  289. */
  290. public static String getSubtractTime(Date time, Integer daysBetween) {
  291. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  292. Calendar rightNow = Calendar.getInstance();
  293. rightNow.setTime(time);
  294. if (daysBetween.intValue() > 0) {
  295. rightNow.add(Calendar.DAY_OF_YEAR, -daysBetween.intValue());
  296. } else {
  297. rightNow.add(Calendar.DAY_OF_YEAR, -1);//日期 -1
  298. }
  299. Date rightNowTime = rightNow.getTime();
  300. String nowTime = sdf.format(rightNowTime);
  301. return nowTime;
  302. }
  303. /**
  304. * 返回 日期-天数 的string 日期
  305. *
  306. * @param time 时间
  307. * @param daysBetween 天数
  308. * @return
  309. */
  310. public static String getSubtractTime2(Date time, Integer daysBetween) {
  311. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  312. Calendar rightNow = Calendar.getInstance();
  313. rightNow.setTime(time);
  314. if (daysBetween.intValue() > 0) {
  315. rightNow.add(Calendar.DAY_OF_YEAR, -daysBetween.intValue() - 1);
  316. } else {
  317. rightNow.add(Calendar.DAY_OF_YEAR, -1);//日期 -1
  318. }
  319. Date rightNowTime = rightNow.getTime();
  320. String nowTime = sdf.format(rightNowTime);
  321. return nowTime;
  322. }
  323. /**
  324. * 根据时间获取季度
  325. * 1即Q1(1,2,3月),以此类推
  326. *
  327. * @param date
  328. * @return
  329. */
  330. public static int getQuarter(Date date) {
  331. Calendar cal = Calendar.getInstance();
  332. cal.setTime(date);
  333. //获取当前月份
  334. int month = cal.get(Calendar.MONTH) + 1;
  335. if (month <= 3) {
  336. return 1;
  337. } else if (month <= 6) {
  338. return 2;
  339. } else if (month <= 9) {
  340. return 3;
  341. } else if (month <= 12) {
  342. return 4;
  343. }
  344. return 0;
  345. }
  346. public static Long getStartLongTime(String orderStartDate) {
  347. String date = orderStartDate + " 00:00:00";
  348. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  349. Long orderCreateTimeStart = null;
  350. try {
  351. orderCreateTimeStart = sdf.parse(date).getTime();
  352. } catch (ParseException e) {
  353. e.printStackTrace();
  354. }
  355. return orderCreateTimeStart;
  356. }
  357. public static Integer getIntegerTime(String date) {
  358. String replace = date.replace("-", "");
  359. return Integer.valueOf(replace);
  360. }
  361. public static Long getEndLongTime(String orderEndDate) {
  362. String date = orderEndDate + " 23:59:59";
  363. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  364. Long orderCreateTimeEnd = null;
  365. try {
  366. orderCreateTimeEnd = sdf.parse(date).getTime();
  367. } catch (ParseException e) {
  368. e.printStackTrace();
  369. }
  370. return orderCreateTimeEnd;
  371. }
  372. public static Integer getNowHour() {
  373. Calendar cal = Calendar.getInstance();
  374. Date date = new Date();
  375. cal.setTime(date);
  376. return cal.get(Calendar.HOUR_OF_DAY);
  377. }
  378. public static String getAnotherDay(String format, String date, Integer num) {
  379. SimpleDateFormat sdf = new SimpleDateFormat(format);
  380. Date getDate = null;
  381. try {
  382. getDate = sdf.parse(date);
  383. } catch (ParseException e) {
  384. e.printStackTrace();
  385. }
  386. Calendar calendar = Calendar.getInstance();
  387. calendar.setTime(getDate);
  388. calendar.add(Calendar.DAY_OF_MONTH, num);
  389. Date resultDate = calendar.getTime();
  390. return sdf.format(resultDate);
  391. }
  392. public static Long getNowEndHourTemp(String dateStr, Integer hour) {
  393. String date = dateStr + " " + hour + ":59:59";
  394. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  395. Long orderCreateTimeEnd = null;
  396. try {
  397. orderCreateTimeEnd = sdf.parse(date).getTime();
  398. } catch (ParseException e) {
  399. e.printStackTrace();
  400. }
  401. return orderCreateTimeEnd;
  402. }
  403. /**
  404. * 获取当前月份第一天
  405. *
  406. * @return 返回格式:2022-01-19
  407. */
  408. public static String getFirstDayByMonth() {
  409. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  410. Calendar c = Calendar.getInstance();
  411. c.add(Calendar.MONTH, 0);
  412. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  413. return format.format(c.getTime());
  414. }
  415. public static String getLastMonthFirstDay() {
  416. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  417. Calendar c = Calendar.getInstance();
  418. c.add(Calendar.MONTH, -1);
  419. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  420. return format.format(c.getTime());
  421. }
  422. public static String getLastMonthFirstDayByDate(String date) throws ParseException {
  423. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  424. Calendar c = Calendar.getInstance();
  425. c.setTime(format.parse(date));
  426. c.add(Calendar.MONTH, -1);
  427. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  428. return format.format(c.getTime());
  429. }
  430. public static String getLastMonthNowDay(String date) throws ParseException {
  431. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  432. Calendar c = Calendar.getInstance();
  433. c.setTime(format.parse(date));
  434. c.add(Calendar.MONTH, -1);
  435. // c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  436. return format.format(c.getTime());
  437. }
  438. public static String getMonthStartDay(String date) throws ParseException {
  439. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  440. Calendar c = Calendar.getInstance();
  441. c.setTime(format.parse(date));
  442. // c.add(Calendar.MONTH, -1);
  443. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  444. return format.format(c.getTime());
  445. }
  446. /**
  447. * 获取 两个日期之间的所有 日期
  448. *
  449. * @param startDate yyyy-MM-dd
  450. * @param endDate yyyy-MM-dd
  451. * @return
  452. */
  453. public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
  454. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  455. List<String> dateList = new ArrayList<String>();
  456. try {
  457. Date dateOne = sdf.parse(startDate);
  458. Date dateTwo = sdf.parse(endDate);
  459. Calendar calendar = Calendar.getInstance();
  460. calendar.setTime(dateOne);
  461. dateList.add(startDate);
  462. while (calendar.getTime().before(dateTwo)) {
  463. calendar.add(Calendar.DAY_OF_MONTH, 1);
  464. dateList.add(sdf.format(calendar.getTime()));
  465. }
  466. } catch (Exception e) {
  467. e.printStackTrace();
  468. }
  469. return dateList;
  470. }
  471. public static String tempToDate(long millis) {
  472. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  473. Date d1 = new Date(millis);
  474. String d2 = simpleDateFormat.format(d1);
  475. return d2;
  476. }
  477. public static Integer getHour(String dateStr) {
  478. try {
  479. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  480. Calendar cal = Calendar.getInstance();
  481. Date date = sdf.parse(dateStr);
  482. cal.setTime(date);
  483. return cal.get(Calendar.HOUR_OF_DAY);
  484. } catch (ParseException e) {
  485. e.printStackTrace();
  486. }
  487. return null;
  488. }
  489. /**
  490. * 根据本阶段日期 获取上阶段 开始 和 截至 日期
  491. *
  492. * @param startTime 本阶段开始时间
  493. * @param endTime 本阶段截至时间
  494. * @return
  495. * @throws ParseException
  496. */
  497. public static Map<String, String> getStartEndTime(String startTime, String endTime) {
  498. Map<String, String> timeMap = new HashMap();
  499. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  500. Date start = null;
  501. Date end = null;
  502. try {
  503. start = sdf.parse(startTime);
  504. end = sdf.parse(endTime);
  505. } catch (ParseException e) {
  506. e.printStackTrace();
  507. }
  508. //相差天数
  509. Long daysBetween = (end.getTime() - start.getTime()) / (60 * 60 * 24 * 1000);
  510. String lastTimeStart = getSubtractTime2(start, daysBetween.intValue());
  511. String lastTimeEnd = getSubtractTime2(end, daysBetween.intValue());
  512. timeMap.put("lastTimeStart", lastTimeStart);
  513. timeMap.put("lastTimeEnd", lastTimeEnd);
  514. timeMap.put("daysBetween", daysBetween.toString());
  515. return timeMap;
  516. }
  517. public static void main(String[] args) {
  518. Map<String, String> map = getStartEndTime("2024-09-12", "2024-09-18");
  519. //下个周期的开始时间
  520. String lastTimeStart = map.get("lastTimeStart");
  521. //下个周期的结束时间
  522. String lastTimeEnd = map.get("lastTimeEnd");
  523. System.out.println(map);
  524. System.out.println(lastTimeStart);
  525. System.out.println(lastTimeEnd);
  526. }
  527. }