DateUtils.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 timestampToDateFormat(Long times,String patternStr){
  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 DateFormatUtils.format(date, patternStr);
  97. }
  98. public static String timestampToDate(Long times){
  99. if(Validator.isEmpty(times)){
  100. return "";
  101. }
  102. long t = times.longValue();
  103. t = t * 1000;
  104. Date date = new Date(t);
  105. return dateTime(date);
  106. }
  107. /**
  108. * 日期路径 即年/月/日 如2018/08/08
  109. */
  110. public static final String datePath()
  111. {
  112. Date now = new Date();
  113. return DateFormatUtils.format(now, "yyyy/MM/dd");
  114. }
  115. /**
  116. * 日期路径 即年/月/日 如20180808
  117. */
  118. public static final String dateTime()
  119. {
  120. Date now = new Date();
  121. return DateFormatUtils.format(now, "yyyyMMdd");
  122. }
  123. /**
  124. * 日期型字符串转化为日期 格式
  125. */
  126. public static Date parseDate(Object str)
  127. {
  128. if (str == null)
  129. {
  130. return null;
  131. }
  132. try
  133. {
  134. return parseDate(str.toString(), parsePatterns);
  135. }
  136. catch (ParseException e)
  137. {
  138. return null;
  139. }
  140. }
  141. /**
  142. * 获取服务器启动时间
  143. */
  144. public static Date getServerStartDate()
  145. {
  146. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  147. return new Date(time);
  148. }
  149. /**
  150. * 计算两个时间差
  151. */
  152. public static String getDatePoor(Date endDate, Date nowDate)
  153. {
  154. long nd = 1000 * 24 * 60 * 60;
  155. long nh = 1000 * 60 * 60;
  156. long nm = 1000 * 60;
  157. // long ns = 1000;
  158. // 获得两个时间的毫秒时间差异
  159. long diff = endDate.getTime() - nowDate.getTime();
  160. // 计算差多少天
  161. long day = diff / nd;
  162. // 计算差多少小时
  163. long hour = diff % nd / nh;
  164. // 计算差多少分钟
  165. long min = diff % nd % nh / nm;
  166. // 计算差多少秒//输出结果
  167. // long sec = diff % nd % nh % nm / ns;
  168. return day + "天" + hour + "小时" + min + "分钟";
  169. }
  170. public static Long getNowTime()
  171. {
  172. return System.currentTimeMillis()/1000;
  173. }
  174. /**
  175. * 获取今天凌晨时间戳
  176. */
  177. public static Long getTodayZeroTime()
  178. {
  179. Calendar cal = Calendar.getInstance();
  180. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  181. return cal.getTimeInMillis() / 1000; //今天凌晨
  182. }
  183. /**
  184. * 获取日期格式订单号
  185. * @return
  186. */
  187. public static String getDateOrderSn()
  188. {
  189. DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  190. String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
  191. //随机数
  192. String randomNumeric = RandomStringUtils.randomNumeric(8);
  193. return localDate+randomNumeric;
  194. }
  195. public static String getDateInputOrderSn()
  196. {
  197. return "LD"+getDateOrderSn();
  198. }
  199. public static String getPayOrderSn()
  200. {
  201. return "P"+getDateOrderSn();
  202. }
  203. public static String secToTime(int time) {
  204. String timeStr = null;
  205. int hour = 0;
  206. int minute = 0;
  207. int second = 0;
  208. if (time <= 0)
  209. return "00:00";
  210. else {
  211. minute = time / 60;
  212. if (minute < 60) {
  213. second = time % 60;
  214. timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
  215. } else {
  216. hour = minute / 60;
  217. if (hour > 99)
  218. return "99:59:59";
  219. minute = minute % 60;
  220. second = time - hour * 3600 - minute * 60;
  221. timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
  222. }
  223. }
  224. return timeStr;
  225. }
  226. public static String unitFormat(int i) {
  227. String retStr = null;
  228. if (i >= 0 && i < 10)
  229. retStr = "0" + Integer.toString(i);
  230. else
  231. retStr = "" + i;
  232. return retStr;
  233. }
  234. public static Integer durationFormat(String duration) {
  235. int index1=duration.indexOf(":");
  236. int index2=duration.indexOf(":",index1+1);
  237. int hh=Integer.parseInt(duration.substring(0,index1));
  238. int mi=Integer.parseInt(duration.substring(index1+1,index2));
  239. int ss=Integer.parseInt(duration.substring(index2+1));
  240. return hh*60*60+mi*60+ss;
  241. }
  242. }