DateUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.zhongzheng.common.utils;
  2. import java.lang.management.ManagementFactory;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.time.LocalDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. import java.util.Date;
  8. import cn.hutool.core.lang.Validator;
  9. import org.apache.commons.lang3.RandomStringUtils;
  10. import org.apache.commons.lang3.time.DateFormatUtils;
  11. /**
  12. * 时间工具类
  13. *
  14. * @author zhongzheng
  15. */
  16. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  17. {
  18. public static String YYYY = "yyyy";
  19. public static String YYYY_MM = "yyyy-MM";
  20. public static String YYYY_MM_DD = "yyyy-MM-dd";
  21. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  22. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  23. private static String[] parsePatterns = {
  24. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  25. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  26. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  27. /**
  28. * 获取当前Date型日期
  29. *
  30. * @return Date() 当前日期
  31. */
  32. public static Date getNowDate()
  33. {
  34. return new Date();
  35. }
  36. /**
  37. * 获取当前日期, 默认格式为yyyy-MM-dd
  38. *
  39. * @return String
  40. */
  41. public static String getDate()
  42. {
  43. return dateTimeNow(YYYY_MM_DD);
  44. }
  45. public static final String getTime()
  46. {
  47. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  48. }
  49. public static final String dateTimeNow()
  50. {
  51. return dateTimeNow(YYYYMMDDHHMMSS);
  52. }
  53. public static final String dateTimeNow(final String format)
  54. {
  55. return parseDateToStr(format, new Date());
  56. }
  57. public static final String dateTime(final Date date)
  58. {
  59. return parseDateToStr(YYYY_MM_DD, date);
  60. }
  61. public static final String parseDateToStr(final String format, final Date date)
  62. {
  63. return new SimpleDateFormat(format).format(date);
  64. }
  65. public static final Date dateTimeThrow(final String format, final String ts) throws ParseException {
  66. return new SimpleDateFormat(format).parse(ts);
  67. }
  68. public static final Date dateTime(final String format, final String ts)
  69. {
  70. try
  71. {
  72. return new SimpleDateFormat(format).parse(ts);
  73. }
  74. catch (ParseException e)
  75. {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. public static String timestampToDateFormat(Long times,String format){
  80. if(Validator.isEmpty(times)){
  81. return "";
  82. }
  83. long t = times.longValue();
  84. t = t * 1000;
  85. Date date = new Date(t);
  86. return DateFormatUtils.format(date,format);
  87. }
  88. public static String timestampToDate(Long times){
  89. if(Validator.isEmpty(times)){
  90. return "";
  91. }
  92. long t = times.longValue();
  93. t = t * 1000;
  94. Date date = new Date(t);
  95. return dateTime(date);
  96. }
  97. /**
  98. * 日期路径 即年/月/日 如2018/08/08
  99. */
  100. public static final String datePath()
  101. {
  102. Date now = new Date();
  103. return DateFormatUtils.format(now, "yyyy/MM/dd");
  104. }
  105. /**
  106. * 日期路径 即年/月/日 如20180808
  107. */
  108. public static final String dateTime()
  109. {
  110. Date now = new Date();
  111. return DateFormatUtils.format(now, "yyyyMMdd");
  112. }
  113. /**
  114. * 日期型字符串转化为日期 格式
  115. */
  116. public static Date parseDate(Object str)
  117. {
  118. if (str == null)
  119. {
  120. return null;
  121. }
  122. try
  123. {
  124. return parseDate(str.toString(), parsePatterns);
  125. }
  126. catch (ParseException e)
  127. {
  128. return null;
  129. }
  130. }
  131. /**
  132. * 获取服务器启动时间
  133. */
  134. public static Date getServerStartDate()
  135. {
  136. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  137. return new Date(time);
  138. }
  139. /**
  140. * 计算两个时间差
  141. */
  142. public static String getDatePoor(Date endDate, Date nowDate)
  143. {
  144. long nd = 1000 * 24 * 60 * 60;
  145. long nh = 1000 * 60 * 60;
  146. long nm = 1000 * 60;
  147. // long ns = 1000;
  148. // 获得两个时间的毫秒时间差异
  149. long diff = endDate.getTime() - nowDate.getTime();
  150. // 计算差多少天
  151. long day = diff / nd;
  152. // 计算差多少小时
  153. long hour = diff % nd / nh;
  154. // 计算差多少分钟
  155. long min = diff % nd % nh / nm;
  156. // 计算差多少秒//输出结果
  157. // long sec = diff % nd % nh % nm / ns;
  158. return day + "天" + hour + "小时" + min + "分钟";
  159. }
  160. public static Long getNowTime()
  161. {
  162. return System.currentTimeMillis()/1000;
  163. }
  164. /**
  165. * 获取日期格式订单号
  166. * @return
  167. */
  168. public static String getDateOrderSn()
  169. {
  170. DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  171. String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
  172. //随机数
  173. String randomNumeric = RandomStringUtils.randomNumeric(8);
  174. return localDate+randomNumeric;
  175. }
  176. }