DateUtils.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. package com.zx.dataservice.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.commons.lang3.time.DateFormatUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.lang.management.ManagementFactory;
  7. import java.text.DateFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. import java.util.TimeZone;
  13. /**
  14. * 时间工具类
  15. *2020-12-12
  16. * @author qlm
  17. */
  18. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  19. private static Logger LOG = LoggerFactory.getLogger(DateUtils.class);
  20. public static String YYYY = "yyyy";
  21. public static String YYYY_MM = "yyyy-MM";
  22. public static String YYYY_MM_DD = "yyyy-MM-dd";
  23. public static String YYYYMMDD = "yyyyMMdd";
  24. public static String HH_MM_SS = "HH:mm:ss";
  25. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  26. public static String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
  27. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  28. public static String YYYY_MM_DD_HH_MM_SS_sss = "yyyy-MM-dd HH:mm:ss.SSS";
  29. private static String[] parsePatterns = {
  30. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  31. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  32. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  33. /**
  34. * 获取当前Date型日期
  35. *
  36. * @return Date() 当前日期
  37. */
  38. public static Date getNowDate() {
  39. return new Date();
  40. }
  41. /**
  42. * 判断当前时间是否在时间段内
  43. *
  44. * @param startTime 开始 20:20
  45. * @param endTime 结束 21:50
  46. * @return
  47. */
  48. public static boolean belongCalendar(String startTime, String endTime) {
  49. SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
  50. Date now = null;
  51. Date beginTimeDate = null;
  52. Date endTimeDate = null;
  53. try {
  54. now = df.parse(df.format(new Date()));
  55. beginTimeDate = df.parse(startTime);
  56. endTimeDate = df.parse(endTime);
  57. } catch (Exception e) {
  58. if(e.toString().length()>500){ LOG.error(e.toString().substring(0,450)); }else { LOG.error(e.toString()); }
  59. }
  60. return belongCalendar(now, beginTimeDate, endTimeDate);
  61. }
  62. /**
  63. * 判断时间是否在时间段内
  64. *
  65. * @param nowTime 目标时间
  66. * @param beginTime 开始时间
  67. * @param endTime 结束时间
  68. * @return
  69. */
  70. public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
  71. Calendar date = Calendar.getInstance();
  72. date.setTime(nowTime);
  73. Calendar begin = Calendar.getInstance();
  74. begin.setTime(beginTime);
  75. Calendar end = Calendar.getInstance();
  76. end.setTime(endTime);
  77. if (date.after(begin) && date.before(end)) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. /**
  84. * 计算两个时间差
  85. */
  86. public static String getDatePoor(Date endDate, Date nowDate) {
  87. long nd = 1000 * 24 * 60 * 60;
  88. long nh = 1000 * 60 * 60;
  89. long nm = 1000 * 60;
  90. // long ns = 1000;
  91. // 获得两个时间的毫秒时间差异
  92. long diff = endDate.getTime() - nowDate.getTime();
  93. // 计算差多少天
  94. long day = diff / nd;
  95. // 计算差多少小时
  96. long hour = diff % nd / nh;
  97. // 计算差多少分钟
  98. long min = diff % nd % nh / nm;
  99. // 计算差多少秒//输出结果
  100. // long sec = diff % nd % nh % nm / ns;
  101. return day + "天" + hour + "小时" + min + "分钟";
  102. }
  103. /**
  104. * 计算两个时间差多少天
  105. */
  106. public static String getDatePoorDay(String endDate, String nowDate) {
  107. long nd = 1000 * 24 * 60 * 60;
  108. long nh = 1000 * 60 * 60;
  109. long nm = 1000 * 60;
  110. // long ns = 1000;
  111. // 获得两个时间的毫秒时间差异
  112. long diff = formatToDate(endDate).getTime() - formatToDate(nowDate).getTime();
  113. // 计算差多少天
  114. long day = diff / nd;
  115. // 计算差多少小时
  116. long hour = diff % nd / nh;
  117. // 计算差多少分钟
  118. long min = diff % nd % nh / nm;
  119. // 计算差多少秒//输出结果
  120. // long sec = diff % nd % nh % nm / ns;
  121. return day +"";
  122. }
  123. /**
  124. * 获取当前日期, 默认格式为yyyy-MM-dd
  125. *
  126. * @return String
  127. */
  128. public static String getDate() {
  129. return dateTimeNow(YYYY_MM_DD);
  130. }
  131. public static String getDate_() {
  132. return dateTimeNow(YYYYMMDD);
  133. }
  134. public static String getDate_(int d) {
  135. addDay(new Date(), d, YYYYMMDD, Calendar.DATE);
  136. return dateTimeNow(YYYYMMDD);
  137. }
  138. public static String getHhMmSs() {
  139. return dateTimeNow(HH_MM_SS);
  140. }
  141. public static String getCurrentTime() {
  142. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  143. }
  144. public synchronized static String getCurrentDay() {
  145. return dateTimeNow(YYYY_MM_DD);
  146. }
  147. public static final String getTime() {
  148. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  149. }
  150. public static final String dateTimeNow() {
  151. return dateTimeNow(YYYYMMDDHHMMSS);
  152. }
  153. public static final String getTimeSSS() {
  154. return dateTimeNow(YYYYMMDDHHMMSSSSS);
  155. }
  156. public static final String dateTimeNow_YYYY_MM_DD_HH_MM_SS() {
  157. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  158. }
  159. public static final String getCurrentTimeSSS() {
  160. return dateTimeNow(YYYY_MM_DD_HH_MM_SS_sss);
  161. }
  162. public static final String dateTimeNow(final String format) {
  163. return parseDateToStr(format, new Date());
  164. }
  165. public static final String dateTime(final Date date) {
  166. return parseDateToStr(YYYY_MM_DD, date);
  167. }
  168. public static final String parseDateToStr(final String format, final Date date) {
  169. return new SimpleDateFormat(format).format(date);
  170. }
  171. public static final Date dateTime(final String format, final String ts) {
  172. try {
  173. return new SimpleDateFormat(format).parse(ts);
  174. } catch (ParseException e) {
  175. throw new RuntimeException(e);
  176. }
  177. }
  178. /**
  179. * 获取服务器启动时间
  180. */
  181. public static Date getServerStartDate() {
  182. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  183. return new Date(time);
  184. }
  185. /**
  186. * 日期型字符串转化为日期 格式
  187. */
  188. public static Date parseDate(Object str) {
  189. if (str == null) {
  190. return null;
  191. }
  192. try {
  193. return parseDate(str.toString(), parsePatterns);
  194. } catch (ParseException e) {
  195. return null;
  196. }
  197. }
  198. public static Date strToDate(String dateStr) {
  199. try {
  200. SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  201. Date date = sDateFormat.parse(dateStr);
  202. return date;
  203. } catch (ParseException e) {
  204. if(e.toString().length()>500){ LOG.error(e.toString().substring(0,450)); }else { LOG.error(e.toString()); }
  205. }
  206. return null;
  207. }
  208. //时间戳转时间
  209. public static String stampToDate(String s, String format) throws Exception {
  210. String res;
  211. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
  212. long lt = new Long(s);
  213. Date date = new Date(lt);
  214. res = simpleDateFormat.format(date);
  215. return res;
  216. }
  217. //获取今日凌晨时间戳
  218. public static long getEarlyThisMorningStamp() {
  219. long nowTime = System.currentTimeMillis();
  220. long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
  221. return todayStartTime;
  222. }
  223. /**
  224. * 日期路径 即年/月/日 如2018/08/08
  225. */
  226. public static final String datePath() {
  227. Date now = new Date();
  228. return DateFormatUtils.format(now, "yyyy/MM/dd");
  229. }
  230. /**
  231. * 日期路径 即年/月/日 如20180808
  232. */
  233. public static final String dateTime() {
  234. Date now = new Date();
  235. return DateFormatUtils.format(now, "yyyyMMdd");
  236. }
  237. /***
  238. * 增加N天 ,可以是负数
  239. * @param date
  240. * @param dayNum
  241. * @param formatStr
  242. * @return
  243. */
  244. public static String addDay(Date date, int dayNum, String formatStr, int type) {
  245. if (StringUtils.isBlank(formatStr)) {
  246. formatStr = YYYY_MM_DD_HH_MM_SS;
  247. }
  248. SimpleDateFormat format = new SimpleDateFormat(formatStr);
  249. Calendar c = Calendar.getInstance();
  250. c.setTime(date);
  251. c.add(type, dayNum);
  252. Date m = c.getTime();
  253. String mon = format.format(m);
  254. return mon;
  255. }
  256. /**
  257. * 增加N天 ,可以是负数
  258. *
  259. * @param date 字符串时间
  260. * @param dayNum 天数
  261. * @return
  262. */
  263. public static String addDay(String date, int dayNum) {
  264. return addDay(formatToDateTime(date), dayNum, null, Calendar.DATE);
  265. }
  266. /**
  267. * 增加n天
  268. *
  269. * @param dayNum
  270. * @return
  271. */
  272. public static String addDay(int dayNum) {
  273. return addDay(new Date(), dayNum, null, Calendar.DATE);
  274. }
  275. public static String addDayYMD(int dayNum) {
  276. return addDay(new Date(), dayNum, YYYY_MM_DD, Calendar.DATE);
  277. }
  278. /***
  279. * 增加N月
  280. * @param month
  281. * @return
  282. */
  283. public static String addMonth(int month) {
  284. return addDay(new Date(), month, null, Calendar.MONTH);
  285. }
  286. /***
  287. * 增加N年
  288. * @param year
  289. * @return
  290. */
  291. public static String addYear(int year) {
  292. return addDay(new Date(), year, null, Calendar.YEAR);
  293. }
  294. /**
  295. * 根据格式化字符串 把时间进行格式化
  296. *
  297. * @param formatStr
  298. * @param date
  299. * @return
  300. */
  301. public synchronized static String formatDate(String formatStr, Date date) {
  302. SimpleDateFormat format = new SimpleDateFormat(formatStr);
  303. String dateStr = format.format(date);
  304. return dateStr;
  305. }
  306. /**
  307. * @param time
  308. * @return
  309. */
  310. public synchronized static Date formatToDate(String time) {
  311. //规范时间格式
  312. SimpleDateFormat pubTimeormatter = new SimpleDateFormat("yyyy-MM-dd");
  313. Date pubTimedate = null;
  314. try {
  315. pubTimedate = pubTimeormatter.parse(time);
  316. } catch (ParseException e) {
  317. pubTimeormatter = new SimpleDateFormat("yyyy/MM/dd");
  318. try {
  319. pubTimedate = pubTimeormatter.parse(time);
  320. } catch (ParseException e1) {
  321. pubTimeormatter = new SimpleDateFormat(HH_MM_SS);
  322. try {
  323. pubTimedate = pubTimeormatter.parse(time);
  324. } catch (ParseException e3) {
  325. e3.printStackTrace();
  326. }
  327. }
  328. }
  329. return pubTimedate;
  330. }
  331. public synchronized static Date formatToDateTime(String time) {
  332. //规范时间格式
  333. SimpleDateFormat pubTimeormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  334. Date pubTimedate = null;
  335. try {
  336. pubTimedate = pubTimeormatter.parse(time);
  337. } catch (ParseException e) {
  338. if(e.toString().length()>500){ LOG.error(e.toString().substring(0,450)); }else { LOG.error(e.toString()); }
  339. }
  340. return pubTimedate;
  341. }
  342. public synchronized static String addDaysWithHms(int days) {
  343. return addDay(new Date(), days, null, Calendar.DATE);
  344. }
  345. public synchronized static int differentDays(Date date1, Date date2) {
  346. Calendar cal1 = Calendar.getInstance();
  347. cal1.setTime(date1);
  348. Calendar cal2 = Calendar.getInstance();
  349. cal2.setTime(date2);
  350. int day1 = cal1.get(Calendar.DAY_OF_YEAR);
  351. int day2 = cal2.get(Calendar.DAY_OF_YEAR);
  352. int year1 = cal1.get(Calendar.YEAR);
  353. int year2 = cal2.get(Calendar.YEAR);
  354. if (year1 != year2) //同一年
  355. {
  356. int timeDistance = 0;
  357. for (int i = year1; i < year2; i++) {
  358. if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) //闰年
  359. {
  360. timeDistance += 366;
  361. } else //不是闰年
  362. {
  363. timeDistance += 365;
  364. }
  365. }
  366. return timeDistance + (day2 - day1);
  367. } else //不同年
  368. {
  369. System.out.println("判断day2 - day1 : " + (day2 - day1));
  370. return day2 - day1;
  371. }
  372. }
  373. //判断当前时间是否在时间date2之前
  374. //时间格式 2005-4-21 16:16:34
  375. public static boolean isDateBefore(String date2) {
  376. try {
  377. Date date1 = new Date();
  378. DateFormat df = DateFormat.getDateTimeInstance();
  379. return date1.before(df.parse(date2 + " 00:00:00"));
  380. } catch (ParseException e) {
  381. System.out.print("[SYS] " + e.getMessage());
  382. return false;
  383. }
  384. }
  385. // 2019-07-10 新增
  386. /**
  387. * 获取任意时间的下一个月的固定天
  388. * @param repeatDate // 时间
  389. * @param format 传递过来的时间格式
  390. * @return
  391. */
  392. public static String getPreMonthDay(String repeatDate,String format) {
  393. Date repeatDateD = dateTime(format,repeatDate);
  394. repeatDate = formatDate("yyyyMMdd",repeatDateD);
  395. String lastMonth = "";
  396. Calendar cal = Calendar.getInstance();
  397. SimpleDateFormat dft = new SimpleDateFormat(format);
  398. int year = Integer.parseInt(repeatDate.substring(0, 4));
  399. String monthsString = repeatDate.substring(4, 6);
  400. int month;
  401. if ("0".equals(monthsString.substring(0, 1))) {
  402. month = Integer.parseInt(monthsString.substring(1, 2));
  403. } else {
  404. month = Integer.parseInt(monthsString.substring(0, 2));
  405. }
  406. int day = Integer.parseInt(repeatDate.substring(6, 8));
  407. cal.set(year,month,day);
  408. lastMonth = dft.format(cal.getTime());
  409. return lastMonth;
  410. }
  411. /**
  412. * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
  413. *
  414. * @param nowTime 当前时间
  415. * @param startTime 开始时间
  416. * @param endTime 结束时间
  417. * @return
  418. * @author jqlin
  419. */
  420. public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
  421. if (nowTime.getTime() == startTime.getTime()
  422. || nowTime.getTime() == endTime.getTime()) {
  423. return true;
  424. }
  425. Calendar date = Calendar.getInstance();
  426. date.setTime(nowTime);
  427. Calendar begin = Calendar.getInstance();
  428. begin.setTime(startTime);
  429. Calendar end = Calendar.getInstance();
  430. end.setTime(endTime);
  431. if (date.after(begin) && date.before(end)) {
  432. return true;
  433. } else {
  434. return false;
  435. }
  436. }
  437. public static boolean isToday(String date) {
  438. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  439. if (date.equals(fmt.format(new Date()).toString())) { //格式化为相同格式
  440. return true;
  441. } else {
  442. return false;
  443. }
  444. }
  445. //获取指定时间的时间戳
  446. public static String timeInMillis(String str){
  447. Date date = formatToDateTime(str);
  448. Calendar cal = Calendar.getInstance();
  449. cal.setTime(date);
  450. long timestamp = cal.getTimeInMillis();
  451. return timestamp +"";
  452. }
  453. // 获取当天0点时间戳
  454. public static Long dayTimeInMillis() {
  455. Calendar calendar = Calendar.getInstance();// 获取当前日期
  456. calendar.set(Calendar.HOUR_OF_DAY, 0);
  457. calendar.set(Calendar.MINUTE, 0);
  458. calendar.set(Calendar.SECOND, 0);
  459. Long time = calendar.getTimeInMillis();
  460. return time;
  461. }
  462. // 获取当天0点时间戳
  463. public static Long dayTimeInMillis(String oldtime) {
  464. Calendar calendar = Calendar.getInstance();// 获取当前日期
  465. calendar.setTime(formatToDate(oldtime));
  466. calendar.set(Calendar.HOUR_OF_DAY, 0);
  467. calendar.set(Calendar.MINUTE, 0);
  468. calendar.set(Calendar.SECOND, 0);
  469. Long time = calendar.getTimeInMillis()/1000;
  470. return time;
  471. }
  472. // 获取当周0点时间戳
  473. public static Long weekTimeInMillis() {
  474. Calendar cal = Calendar.getInstance();
  475. cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
  476. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  477. return (cal.getTimeInMillis()/1000);
  478. }
  479. // 获取指定周0点时间戳
  480. public static Long weekTimeInMillis(String oldtime) {
  481. Calendar cal = Calendar.getInstance();
  482. cal.setTime(formatToDate(oldtime));
  483. cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
  484. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  485. return (cal.getTimeInMillis()/1000);
  486. }
  487. // 获取当月0点时间戳
  488. public static Long monthTimeInMillis() {
  489. Calendar calendar = Calendar.getInstance();// 获取当前日期
  490. calendar.add(Calendar.YEAR, 0);
  491. calendar.add(Calendar.MONTH, 0);
  492. calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
  493. calendar.set(Calendar.HOUR_OF_DAY, 0);
  494. calendar.set(Calendar.MINUTE, 0);
  495. calendar.set(Calendar.SECOND, 0);
  496. Long time = calendar.getTimeInMillis()/1000;
  497. return time;
  498. }
  499. // 获取当月0点时间戳
  500. public static Long monthTimeInMillis(String oldtime) {
  501. Calendar calendar = Calendar.getInstance();// 获取当前日期
  502. calendar.setTime(formatToDate(oldtime));
  503. calendar.add(Calendar.YEAR, 0);
  504. calendar.add(Calendar.MONTH, 0);
  505. calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
  506. calendar.set(Calendar.HOUR_OF_DAY, 0);
  507. calendar.set(Calendar.MINUTE, 0);
  508. calendar.set(Calendar.SECOND, 0);
  509. Long time = calendar.getTimeInMillis()/1000;
  510. return time;
  511. }
  512. // 获取当年0点时间戳
  513. public static Long yearTimeInMillis() {
  514. Calendar calendar = Calendar.getInstance();// 获取当前日期
  515. calendar.add(Calendar.YEAR, 0);
  516. calendar.add(Calendar.DATE, 0);
  517. calendar.add(Calendar.MONTH, 0);
  518. calendar.set(Calendar.DAY_OF_YEAR, 1);
  519. calendar.set(Calendar.HOUR, 0);
  520. calendar.set(Calendar.MINUTE, 0);
  521. calendar.set(Calendar.SECOND, 0);
  522. Long time = calendar.getTimeInMillis()/1000;
  523. return time;
  524. }
  525. // 获取当年0点时间戳
  526. public static Long yearTimeInMillis(String oldtime) {
  527. Calendar calendar = Calendar.getInstance();// 获取当前日期
  528. calendar.setTime(formatToDate(oldtime));
  529. calendar.add(Calendar.YEAR, 0);
  530. calendar.add(Calendar.DATE, 0);
  531. calendar.add(Calendar.MONTH, 0);
  532. calendar.set(Calendar.DAY_OF_YEAR, 1);
  533. calendar.set(Calendar.HOUR, 0);
  534. calendar.set(Calendar.MINUTE, 0);
  535. calendar.set(Calendar.SECOND, 0);
  536. Long time = calendar.getTimeInMillis()/1000;
  537. return time;
  538. }
  539. }