DateUtils.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.Calendar;
  8. import java.util.Date;
  9. import cn.hutool.core.lang.Validator;
  10. import org.apache.commons.lang3.RandomStringUtils;
  11. import org.apache.commons.lang3.time.DateFormatUtils;
  12. /**
  13. * 时间工具类
  14. *
  15. * @author zhongzheng
  16. */
  17. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  18. {
  19. public static String YYYY = "yyyy";
  20. public static String YYYY_MM = "yyyy-MM";
  21. public static String YYYY_MM_DD = "yyyy-MM-dd";
  22. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  23. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  24. private static String[] parsePatterns = {
  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. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  28. /**
  29. * 获取当前Date型日期
  30. *
  31. * @return Date() 当前日期
  32. */
  33. public static Date getNowDate()
  34. {
  35. return new Date();
  36. }
  37. /**
  38. * 获取当前日期, 默认格式为yyyy-MM-dd
  39. *
  40. * @return String
  41. */
  42. public static String getDate()
  43. {
  44. return dateTimeNow(YYYY_MM_DD);
  45. }
  46. public static final String getTime()
  47. {
  48. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  49. }
  50. public static final String dateTimeNow()
  51. {
  52. return dateTimeNow(YYYYMMDDHHMMSS);
  53. }
  54. public static final String dateTimeNow(final String format)
  55. {
  56. return parseDateToStr(format, new Date());
  57. }
  58. public static final String dateTime(final Date date)
  59. {
  60. return parseDateToStr(YYYY_MM_DD, date);
  61. }
  62. public static final String parseDateToStr(final String format, final Date date)
  63. {
  64. return new SimpleDateFormat(format).format(date);
  65. }
  66. public static final Date dateTimeThrow(final String format, final String ts) throws ParseException {
  67. return new SimpleDateFormat(format).parse(ts);
  68. }
  69. public static final Date dateTime(final String format, final String ts)
  70. {
  71. try
  72. {
  73. return new SimpleDateFormat(format).parse(ts);
  74. }
  75. catch (ParseException e)
  76. {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. public static String timestampToDateFormat(Long times){
  81. if(Validator.isEmpty(times)){
  82. return "";
  83. }
  84. long t = times.longValue();
  85. t = t * 1000;
  86. Date date = new Date(t);
  87. return DateFormatUtils.format(date, "yyyy/MM/dd");
  88. }
  89. public static String timestampToDate(Long times){
  90. if(Validator.isEmpty(times)){
  91. return "";
  92. }
  93. long t = times.longValue();
  94. t = t * 1000;
  95. Date date = new Date(t);
  96. return dateTime(date);
  97. }
  98. /**
  99. * 日期路径 即年/月/日 如2018/08/08
  100. */
  101. public static final String datePath()
  102. {
  103. Date now = new Date();
  104. return DateFormatUtils.format(now, "yyyy/MM/dd");
  105. }
  106. /**
  107. * 日期路径 即年/月/日 如20180808
  108. */
  109. public static final String dateTime()
  110. {
  111. Date now = new Date();
  112. return DateFormatUtils.format(now, "yyyyMMdd");
  113. }
  114. /**
  115. * 日期型字符串转化为日期 格式
  116. */
  117. public static Date parseDate(Object str)
  118. {
  119. if (str == null)
  120. {
  121. return null;
  122. }
  123. try
  124. {
  125. return parseDate(str.toString(), parsePatterns);
  126. }
  127. catch (ParseException e)
  128. {
  129. return null;
  130. }
  131. }
  132. /**
  133. * 获取服务器启动时间
  134. */
  135. public static Date getServerStartDate()
  136. {
  137. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  138. return new Date(time);
  139. }
  140. /**
  141. * 计算两个时间差
  142. */
  143. public static String getDatePoor(Date endDate, Date nowDate)
  144. {
  145. long nd = 1000 * 24 * 60 * 60;
  146. long nh = 1000 * 60 * 60;
  147. long nm = 1000 * 60;
  148. // long ns = 1000;
  149. // 获得两个时间的毫秒时间差异
  150. long diff = endDate.getTime() - nowDate.getTime();
  151. // 计算差多少天
  152. long day = diff / nd;
  153. // 计算差多少小时
  154. long hour = diff % nd / nh;
  155. // 计算差多少分钟
  156. long min = diff % nd % nh / nm;
  157. // 计算差多少秒//输出结果
  158. // long sec = diff % nd % nh % nm / ns;
  159. return day + "天" + hour + "小时" + min + "分钟";
  160. }
  161. public static Long getNowTime()
  162. {
  163. return System.currentTimeMillis()/1000;
  164. }
  165. /**
  166. * 获取今天凌晨时间戳
  167. */
  168. public static Long getTodayZeroTime()
  169. {
  170. Calendar cal = Calendar.getInstance();
  171. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  172. return cal.getTimeInMillis() / 1000; //今天凌晨
  173. }
  174. /**
  175. * 获取日期格式订单号
  176. * @return
  177. */
  178. public static String getDateOrderSn()
  179. {
  180. DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  181. String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
  182. //随机数
  183. String randomNumeric = RandomStringUtils.randomNumeric(8);
  184. return localDate+randomNumeric;
  185. }
  186. public static String getDateInputOrderSn()
  187. {
  188. return "LD"+getDateOrderSn();
  189. }
  190. public static String getPayOrderSn()
  191. {
  192. return "P"+getDateOrderSn();
  193. }
  194. public static String secToTime(int time) {
  195. String timeStr = null;
  196. int hour = 0;
  197. int minute = 0;
  198. int second = 0;
  199. if (time <= 0)
  200. return "00:00";
  201. else {
  202. minute = time / 60;
  203. if (minute < 60) {
  204. second = time % 60;
  205. timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
  206. } else {
  207. hour = minute / 60;
  208. if (hour > 99)
  209. return "99:59:59";
  210. minute = minute % 60;
  211. second = time - hour * 3600 - minute * 60;
  212. timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
  213. }
  214. }
  215. return timeStr;
  216. }
  217. public static String unitFormat(int i) {
  218. String retStr = null;
  219. if (i >= 0 && i < 10)
  220. retStr = "0" + Integer.toString(i);
  221. else
  222. retStr = "" + i;
  223. return retStr;
  224. }
  225. public static Integer durationFormat(String duration) {
  226. int index1=duration.indexOf(":");
  227. int index2=duration.indexOf(":",index1+1);
  228. int hh=Integer.parseInt(duration.substring(0,index1));
  229. int mi=Integer.parseInt(duration.substring(index1+1,index2));
  230. int ss=Integer.parseInt(duration.substring(index2+1));
  231. return hh*60*60+mi*60+ss;
  232. }
  233. }