DateUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. * 返回 日期-天数 的string 日期
  265. *
  266. * @param time 时间
  267. * @param daysBetween 天数
  268. * @return
  269. */
  270. public static String getSubtractTime(Date time, Integer daysBetween) {
  271. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  272. Calendar rightNow = Calendar.getInstance();
  273. rightNow.setTime(time);
  274. if (daysBetween.intValue() > 0) {
  275. rightNow.add(Calendar.DAY_OF_YEAR, -daysBetween.intValue());
  276. } else {
  277. rightNow.add(Calendar.DAY_OF_YEAR, -1);//日期 -1
  278. }
  279. Date rightNowTime = rightNow.getTime();
  280. String nowTime = sdf.format(rightNowTime);
  281. return nowTime;
  282. }
  283. /**
  284. * 根据时间获取季度
  285. * 1即Q1(1,2,3月),以此类推
  286. *
  287. * @param date
  288. * @return
  289. */
  290. public static int getQuarter(Date date) {
  291. Calendar cal = Calendar.getInstance();
  292. cal.setTime(date);
  293. //获取当前月份
  294. int month = cal.get(Calendar.MONTH) + 1;
  295. if (month <= 3) {
  296. return 1;
  297. } else if (month <= 6) {
  298. return 2;
  299. } else if (month <= 9) {
  300. return 3;
  301. } else if (month <= 12) {
  302. return 4;
  303. }
  304. return 0;
  305. }
  306. public static Long getStartLongTime(String orderStartDate) {
  307. String date = orderStartDate + " 00:00:00";
  308. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  309. Long orderCreateTimeStart = null;
  310. try {
  311. orderCreateTimeStart = sdf.parse(date).getTime();
  312. } catch (ParseException e) {
  313. e.printStackTrace();
  314. }
  315. return orderCreateTimeStart;
  316. }
  317. public static Integer getIntegerTime(String date) {
  318. String replace = date.replace("-", "");
  319. return Integer.valueOf(replace);
  320. }
  321. public static Long getEndLongTime(String orderEndDate) {
  322. String date = orderEndDate + " 23:59:59";
  323. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  324. Long orderCreateTimeEnd = null;
  325. try {
  326. orderCreateTimeEnd = sdf.parse(date).getTime();
  327. } catch (ParseException e) {
  328. e.printStackTrace();
  329. }
  330. return orderCreateTimeEnd;
  331. }
  332. public static Integer getNowHour() {
  333. Calendar cal = Calendar.getInstance();
  334. Date date = new Date();
  335. cal.setTime(date);
  336. return cal.get(Calendar.HOUR_OF_DAY);
  337. }
  338. public static String getAnotherDay(String format, String date, Integer num) {
  339. SimpleDateFormat sdf = new SimpleDateFormat(format);
  340. Date getDate = null;
  341. try {
  342. getDate = sdf.parse(date);
  343. } catch (ParseException e) {
  344. e.printStackTrace();
  345. }
  346. Calendar calendar = Calendar.getInstance();
  347. calendar.setTime(getDate);
  348. calendar.add(Calendar.DAY_OF_MONTH, num);
  349. Date resultDate = calendar.getTime();
  350. return sdf.format(resultDate);
  351. }
  352. public static Long getNowEndHourTemp(String dateStr, Integer hour) {
  353. String date = dateStr + " " + hour + ":59:59";
  354. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  355. Long orderCreateTimeEnd = null;
  356. try {
  357. orderCreateTimeEnd = sdf.parse(date).getTime();
  358. } catch (ParseException e) {
  359. e.printStackTrace();
  360. }
  361. return orderCreateTimeEnd;
  362. }
  363. /**
  364. * 获取当前月份第一天
  365. *
  366. * @return 返回格式:2022-01-19
  367. */
  368. public static String getFirstDayByMonth() {
  369. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  370. Calendar c = Calendar.getInstance();
  371. c.add(Calendar.MONTH, 0);
  372. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  373. return format.format(c.getTime());
  374. }
  375. public static String getLastMonthFirstDay() {
  376. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  377. Calendar c = Calendar.getInstance();
  378. c.add(Calendar.MONTH, -1);
  379. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  380. return format.format(c.getTime());
  381. }
  382. public static String getLastMonthFirstDayByDate(String date) throws ParseException {
  383. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  384. Calendar c = Calendar.getInstance();
  385. c.setTime(format.parse(date));
  386. c.add(Calendar.MONTH, -1);
  387. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  388. return format.format(c.getTime());
  389. }
  390. public static String getLastMonthNowDay(String date) throws ParseException {
  391. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  392. Calendar c = Calendar.getInstance();
  393. c.setTime(format.parse(date));
  394. c.add(Calendar.MONTH, -1);
  395. // c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  396. return format.format(c.getTime());
  397. }
  398. public static String getMonthStartDay(String date) throws ParseException {
  399. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  400. Calendar c = Calendar.getInstance();
  401. c.setTime(format.parse(date));
  402. // c.add(Calendar.MONTH, -1);
  403. c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  404. return format.format(c.getTime());
  405. }
  406. /**
  407. * 获取 两个日期之间的所有 日期
  408. *
  409. * @param startDate yyyy-MM-dd
  410. * @param endDate yyyy-MM-dd
  411. * @return
  412. */
  413. public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
  414. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  415. List<String> dateList = new ArrayList<String>();
  416. try {
  417. Date dateOne = sdf.parse(startDate);
  418. Date dateTwo = sdf.parse(endDate);
  419. Calendar calendar = Calendar.getInstance();
  420. calendar.setTime(dateOne);
  421. dateList.add(startDate);
  422. while (calendar.getTime().before(dateTwo)) {
  423. calendar.add(Calendar.DAY_OF_MONTH, 1);
  424. dateList.add(sdf.format(calendar.getTime()));
  425. }
  426. } catch (Exception e) {
  427. e.printStackTrace();
  428. }
  429. return dateList;
  430. }
  431. public static String tempToDate(long millis) {
  432. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  433. Date d1 = new Date(millis);
  434. String d2 = simpleDateFormat.format(d1);
  435. return d2;
  436. }
  437. public static Integer getHour(String dateStr) {
  438. try {
  439. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  440. Calendar cal = Calendar.getInstance();
  441. Date date = sdf.parse(dateStr);
  442. cal.setTime(date);
  443. return cal.get(Calendar.HOUR_OF_DAY);
  444. } catch (ParseException e) {
  445. e.printStackTrace();
  446. }
  447. return null;
  448. }
  449. public static void main(String[] args) {
  450. System.out.println(getHour("2023-02-01 23:10:12"));
  451. }
  452. }