DateUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package com.gyee.impala.common.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.time.DateFormatUtils;
  4. import org.apache.commons.lang3.time.DateUtils;
  5. import java.lang.management.ManagementFactory;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.TimeZone;
  11. @Slf4j
  12. public class DateUtil extends DateUtils {
  13. /**
  14. * 获取当前时间
  15. *
  16. * @return
  17. */
  18. public static String getCurrentDate() {
  19. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  20. String date = df.format(new Date());
  21. return date;
  22. }
  23. /**
  24. * 获取前 N 小时的时间
  25. *
  26. * @param hour
  27. * @return
  28. */
  29. public static String getPreviousDate(int hour) {
  30. Calendar calendar = Calendar.getInstance();
  31. calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
  32. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  33. String date = df.format(calendar.getTime());
  34. return date;
  35. }
  36. /**
  37. * 获取当前时间后 N 小时的时间
  38. *
  39. * @param hour
  40. * @return
  41. */
  42. public static String getNextDate(int hour) {
  43. Calendar calendar = Calendar.getInstance();
  44. calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
  45. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  46. String date = df.format(calendar.getTime());
  47. return date;
  48. }
  49. /**
  50. * 获取当前时间后 N 小时的时间
  51. *
  52. * @param hour
  53. * @return
  54. */
  55. public static Long getNextDateTimestamp(int hour) {
  56. Calendar calendar = Calendar.getInstance();
  57. calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
  58. long time = calendar.getTime().getTime();
  59. return time;
  60. }
  61. /**
  62. * 字符串时间转时间戳
  63. * @param time
  64. * @return
  65. */
  66. public static Long covertDateTimestamp(String time){
  67. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  68. long stamp = 0;
  69. try {
  70. stamp = sdf.parse(time).getTime();
  71. } catch (ParseException e) {
  72. e.printStackTrace();
  73. }
  74. return stamp;
  75. }
  76. /**
  77. * 时间格式(yyyy-MM-dd)
  78. */
  79. public final static String DATE_PATTERN = "yyyy-MM-dd";
  80. /**
  81. * 时间格式(yyyy-MM-dd HH:mm:ss)
  82. */
  83. public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
  84. public static String format(Date date) {
  85. return format(date, DATE_PATTERN);
  86. }
  87. public static String formatDateTime(Date date) {
  88. return format(date, DATE_TIME_PATTERN);
  89. }
  90. /**
  91. * 时间转换
  92. *
  93. * @param date
  94. * @param pattern
  95. * @return
  96. */
  97. public static String format(Date date, String pattern) {
  98. if (date != null) {
  99. SimpleDateFormat df = new SimpleDateFormat(pattern);
  100. return df.format(date);
  101. }
  102. return null;
  103. }
  104. /**
  105. * 字符串格式化
  106. * @param time
  107. * @param pattern
  108. * @return
  109. */
  110. public static String format(String time,String pattern) {
  111. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  112. Date dateTime = null;
  113. try {
  114. dateTime = sdf.parse(time);
  115. } catch (ParseException e) {
  116. e.printStackTrace();
  117. }
  118. return format(dateTime, pattern);
  119. }
  120. /**
  121. * 时间转换
  122. *
  123. * @param time
  124. * @param pattern
  125. * @return
  126. */
  127. public static String format(Long time, String pattern) {
  128. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  129. Date date = new Date(time);
  130. String format = sdf.format(date);
  131. return format;
  132. }
  133. public static String YYYY = "yyyy";
  134. public static String YYYY_MM = "yyyy-MM";
  135. public static String YYYY_MM_DD = "yyyy-MM-dd";
  136. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  137. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  138. private static String[] parsePatterns = {
  139. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  140. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  141. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  142. /**
  143. * 获取当前Date型日期
  144. *
  145. * @return Date() 当前日期
  146. */
  147. public static Date getNowDate() {
  148. return new Date();
  149. }
  150. /**
  151. * 获取当前日期, 默认格式为yyyy-MM-dd
  152. *
  153. * @return String
  154. */
  155. public static String getDate() {
  156. return dateTimeNow(YYYY_MM_DD);
  157. }
  158. public static final String getTime() {
  159. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  160. }
  161. public static final String dateTimeNow() {
  162. return dateTimeNow(YYYYMMDDHHMMSS);
  163. }
  164. public static final String dateTimeNow(final String format) {
  165. return parseDateToStr(format, new Date());
  166. }
  167. public static final String dateTime(final Date date) {
  168. return parseDateToStr(YYYY_MM_DD, date);
  169. }
  170. public static final String parseDateToStr(final String format, final Date date) {
  171. return new SimpleDateFormat(format).format(date);
  172. }
  173. public static final Date dateTime(final String format, final String ts) {
  174. try {
  175. return new SimpleDateFormat(format).parse(ts);
  176. } catch (ParseException e) {
  177. throw new RuntimeException(e);
  178. }
  179. }
  180. /**
  181. * 日期路径 即年/月/日 如2018/08/08
  182. */
  183. public static final String datePath() {
  184. Date now = new Date();
  185. return DateFormatUtils.format(now, "yyyy/MM/dd");
  186. }
  187. /**
  188. * 日期路径 即年/月/日 如20180808
  189. */
  190. public static final String dateTime() {
  191. Date now = new Date();
  192. return DateFormatUtils.format(now, "yyyyMMdd");
  193. }
  194. /**
  195. * 日期型字符串转化为日期 格式
  196. */
  197. public static Date parseDate(Object str) {
  198. if (str == null) {
  199. return null;
  200. }
  201. try {
  202. return parseDate(str.toString(), parsePatterns);
  203. } catch (ParseException e) {
  204. return null;
  205. }
  206. }
  207. /**
  208. * 获取服务器启动时间
  209. */
  210. public static Date getServerStartDate() {
  211. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  212. return new Date(time);
  213. }
  214. /**
  215. * 计算两个时间差
  216. */
  217. public static String getDatePoor(Date endDate, Date nowDate) {
  218. long nd = 1000 * 24 * 60 * 60;
  219. long nh = 1000 * 60 * 60;
  220. long nm = 1000 * 60;
  221. // long ns = 1000;
  222. // 获得两个时间的毫秒时间差异
  223. long diff = endDate.getTime() - nowDate.getTime();
  224. // 计算差多少天
  225. long day = diff / nd;
  226. // 计算差多少小时
  227. long hour = diff % nd / nh;
  228. // 计算差多少分钟
  229. long min = diff % nd % nh / nm;
  230. // 计算差多少秒//输出结果
  231. // long sec = diff % nd % nh % nm / ns;
  232. return day + "天" + hour + "小时" + min + "分钟";
  233. }
  234. /**
  235. * 将时间的时分秒毫秒字段去掉
  236. *
  237. * @param date
  238. * @return
  239. */
  240. public static Date truncate(Date date) {
  241. Calendar cal = Calendar.getInstance();
  242. cal.setTime(date);
  243. cal.set(Calendar.HOUR_OF_DAY, 0);
  244. cal.set(Calendar.MINUTE, 0);
  245. cal.set(Calendar.SECOND, 0);
  246. cal.set(Calendar.MILLISECOND, 0);
  247. return cal.getTime();
  248. }
  249. /**
  250. * 计算两个时间之间差的天数(取整后)
  251. *
  252. * @param d1
  253. * @param d2
  254. * @return
  255. */
  256. public static int daysDiff(Date d1, Date d2) {
  257. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 24 * 1000));
  258. }
  259. /**
  260. * 将字符串日期转换成日期类型
  261. *
  262. * @param time
  263. * @return
  264. */
  265. public static Date parseStrtoDate(String time) {
  266. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  267. Date dateTime = null;
  268. try {
  269. dateTime = sdf.parse(time);
  270. } catch (ParseException e) {
  271. e.printStackTrace();
  272. }
  273. return dateTime;
  274. }
  275. public static Date parseStrtoDate(String time,String pattern) {
  276. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  277. Date dateTime = null;
  278. try {
  279. dateTime = sdf.parse(time);
  280. } catch (ParseException e) {
  281. e.printStackTrace();
  282. }
  283. return dateTime;
  284. }
  285. public static Date str2DateTime(String time,String pattern) {
  286. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  287. Date dateTime = null;
  288. try {
  289. dateTime = sdf.parse(time);
  290. } catch (ParseException e) {
  291. e.printStackTrace();
  292. }
  293. return dateTime;
  294. }
  295. public static Date str2DateTime(String time) {
  296. return str2DateTime(time,DATE_TIME_PATTERN);
  297. }
  298. /**
  299. * 加减年份
  300. */
  301. public static Date dateTimeAddYear(Date date,int k) {
  302. Calendar instance = Calendar.getInstance();
  303. instance.setTime(date);
  304. instance.add(Calendar.YEAR,k);
  305. return instance.getTime();
  306. }
  307. public static String datetime2Str(Date time,String pattern) {
  308. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  309. return sdf.format(time);
  310. }
  311. public static String datetime2Str(Date time) {
  312. return datetime2Str(time,DATE_TIME_PATTERN);
  313. }
  314. public static double hoursDiff1(Date d1, Date d2) {
  315. return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
  316. }
  317. public static double hoursDiff2(Date d1, Date d2) {
  318. return Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000);
  319. }
  320. /**
  321. * 计算两个时间之间差的分钟数(取整后)
  322. *
  323. * @param d1
  324. * @param d2
  325. * @return
  326. */
  327. public static int minutesDiff(Date d1, Date d2) {
  328. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
  329. }
  330. /**
  331. * 计算两个时间之间差的分钟数(取整后)
  332. *
  333. * @param d1
  334. * @param d2
  335. * @return
  336. */
  337. public static double minutesDiff2(Date d1, Date d2) {
  338. return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
  339. }
  340. /**
  341. * 获取系统时间
  342. *
  343. * @return
  344. */
  345. public static Date now() {
  346. return new Date();
  347. }
  348. /**
  349. * 获取当前月的第一天
  350. *
  351. * @return
  352. */
  353. public static String getCurrtenFirstDay() {
  354. Calendar c = Calendar.getInstance();
  355. // c.add(Calendar.MONTH, 0);
  356. c.set(Calendar.DAY_OF_MONTH, 1);
  357. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  358. }
  359. /**
  360. * 获取当前月的最后一天
  361. *
  362. * @return
  363. */
  364. public static String getCurrtenLastDay() {
  365. Calendar ca = Calendar.getInstance();
  366. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  367. return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime());
  368. }
  369. /**
  370. * 计算两个时间之间差的小时数(取整后)
  371. *
  372. * @param d1
  373. * @param d2
  374. * @return
  375. */
  376. public static int hoursDiff(Date d1, Date d2) {
  377. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 1000));
  378. }
  379. /**
  380. * 获取当前整点数
  381. *
  382. * @return
  383. */
  384. public static Date getCurrHourTime(Date date) {
  385. Calendar ca = Calendar.getInstance();
  386. int minute = ca.get(Calendar.MINUTE);
  387. if (minute < 30) {
  388. ca.set(Calendar.MINUTE, 30);
  389. } else {
  390. ca.add(Calendar.HOUR, 1);
  391. ca.set(Calendar.MINUTE, 0);
  392. }
  393. ca.set(Calendar.SECOND, 5);
  394. date = ca.getTime();
  395. return date;
  396. }
  397. //普通时间转为UTC
  398. public static String localToUTC(String localTimeStr) {
  399. try {
  400. Date localDate = getLocalSDF().parse(localTimeStr);
  401. return getUTCSDF().format(localDate);
  402. } catch (ParseException e) {
  403. e.printStackTrace();
  404. }
  405. return null;
  406. }
  407. //UTC转为普通时间
  408. public static String utcToLocal(String utcTimeStr) {
  409. try {
  410. Date date = getUTCSDF().parse(utcTimeStr);
  411. return getLocalSDF().format(date);
  412. } catch (ParseException e) {
  413. e.printStackTrace();
  414. }
  415. return null;
  416. }
  417. private static SimpleDateFormat getLocalSDF() {
  418. return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  419. }
  420. private static SimpleDateFormat getUTCSDF() {
  421. SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  422. utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
  423. return utcSDF;
  424. }
  425. public static void main(String[] args) {
  426. System.out.println(getCurrentDate());
  427. System.out.println(getPreviousDate(3 * 360 * 24));
  428. System.out.println(covertDateTimestamp("2021-12-30 00:00:00"));
  429. }
  430. /**
  431. * @return 当天零点
  432. */
  433. public static Date today0am() {
  434. Calendar day = Calendar.getInstance();
  435. day.set(Calendar.HOUR_OF_DAY, 0);
  436. day.set(Calendar.MINUTE, 0);
  437. day.set(Calendar.SECOND, 0);
  438. day.set(Calendar.MILLISECOND, 0);
  439. return day.getTime();
  440. }
  441. }