DateUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 java.util.HashMap;
  10. import java.util.Map;
  11. import cn.hutool.core.lang.Validator;
  12. import org.apache.commons.lang3.RandomStringUtils;
  13. import org.apache.commons.lang3.time.DateFormatUtils;
  14. /**
  15. * 时间工具类
  16. *
  17. * @author zhongzheng
  18. */
  19. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  20. {
  21. public static String YYYY = "yyyy";
  22. public static String YYYY_MM = "yyyy-MM";
  23. public static String YYYY_MM_DD = "yyyy-MM-dd";
  24. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  25. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  26. private static String[] parsePatterns = {
  27. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  28. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  29. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  30. /**
  31. * 获取当前Date型日期
  32. *
  33. * @return Date() 当前日期
  34. */
  35. public static Date getNowDate()
  36. {
  37. return new Date();
  38. }
  39. /**
  40. * 获取当前日期, 默认格式为yyyy-MM-dd
  41. *
  42. * @return String
  43. */
  44. public static String getDate()
  45. {
  46. return dateTimeNow(YYYY_MM_DD);
  47. }
  48. public static final String getTime()
  49. {
  50. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  51. }
  52. public static final String dateTimeNow()
  53. {
  54. return dateTimeNow(YYYYMMDDHHMMSS);
  55. }
  56. public static final String dateTimeNow(final String format)
  57. {
  58. return parseDateToStr(format, new Date());
  59. }
  60. public static final String dateTime(final Date date)
  61. {
  62. return parseDateToStr(YYYY_MM_DD, date);
  63. }
  64. public static final String parseDateToStr(final String format, final Date date)
  65. {
  66. return new SimpleDateFormat(format).format(date);
  67. }
  68. public static final Date dateTimeThrow(final String format, final String ts) throws ParseException {
  69. return new SimpleDateFormat(format).parse(ts);
  70. }
  71. public static final Date dateTime(final String format, final String ts)
  72. {
  73. try
  74. {
  75. return new SimpleDateFormat(format).parse(ts);
  76. }
  77. catch (ParseException e)
  78. {
  79. throw new RuntimeException(e);
  80. }
  81. }
  82. public static final Long dateTimeSec(final String format, final String ts)
  83. {
  84. try
  85. {
  86. return (new SimpleDateFormat(format).parse(ts)).getTime()/1000;
  87. }
  88. catch (ParseException e)
  89. {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. public static String timestampToDateFormat(Long times){
  94. if(Validator.isEmpty(times)){
  95. return "";
  96. }
  97. long t = times.longValue();
  98. t = t * 1000;
  99. Date date = new Date(t);
  100. return DateFormatUtils.format(date, "yyyy/MM/dd");
  101. }
  102. public static String timestampToDateFormat(Long times,String patternStr){
  103. if(Validator.isEmpty(times)){
  104. return "";
  105. }
  106. long t = times.longValue();
  107. t = t * 1000;
  108. Date date = new Date(t);
  109. return DateFormatUtils.format(date, patternStr);
  110. }
  111. public static String timestampToDate(Long times){
  112. if(Validator.isEmpty(times)){
  113. return "";
  114. }
  115. long t = times.longValue();
  116. t = t * 1000;
  117. Date date = new Date(t);
  118. return dateTime(date);
  119. }
  120. /**
  121. * 日期路径 即年/月/日 如2018/08/08
  122. */
  123. public static final String datePath()
  124. {
  125. Date now = new Date();
  126. return DateFormatUtils.format(now, "yyyy/MM/dd");
  127. }
  128. /**
  129. * 日期路径 即年/月/日 如20180808
  130. */
  131. public static final String dateTime()
  132. {
  133. Date now = new Date();
  134. return DateFormatUtils.format(now, "yyyyMMdd");
  135. }
  136. /**
  137. * 日期型字符串转化为日期 格式
  138. */
  139. public static Date parseDate(Object str)
  140. {
  141. if (str == null)
  142. {
  143. return null;
  144. }
  145. try
  146. {
  147. return parseDate(str.toString(), parsePatterns);
  148. }
  149. catch (ParseException e)
  150. {
  151. return null;
  152. }
  153. }
  154. /**
  155. * 获取服务器启动时间
  156. */
  157. public static Date getServerStartDate()
  158. {
  159. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  160. return new Date(time);
  161. }
  162. /**
  163. * 计算两个时间差
  164. */
  165. public static String getDatePoor(Date endDate, Date nowDate)
  166. {
  167. long nd = 1000 * 24 * 60 * 60;
  168. long nh = 1000 * 60 * 60;
  169. long nm = 1000 * 60;
  170. // long ns = 1000;
  171. // 获得两个时间的毫秒时间差异
  172. long diff = endDate.getTime() - nowDate.getTime();
  173. // 计算差多少天
  174. long day = diff / nd;
  175. // 计算差多少小时
  176. long hour = diff % nd / nh;
  177. // 计算差多少分钟
  178. long min = diff % nd % nh / nm;
  179. // 计算差多少秒//输出结果
  180. // long sec = diff % nd % nh % nm / ns;
  181. return day + "天" + hour + "小时" + min + "分钟";
  182. }
  183. public static Long getNowTime()
  184. {
  185. return System.currentTimeMillis()/1000;
  186. }
  187. /**
  188. * 获取今天凌晨时间戳
  189. */
  190. public static Long getTodayZeroTime()
  191. {
  192. Calendar cal = Calendar.getInstance();
  193. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  194. return cal.getTimeInMillis() / 1000; //今天凌晨
  195. }
  196. /**
  197. * 获取明天凌晨时间戳
  198. */
  199. public static Long getTomorrowZeroTime()
  200. {
  201. Calendar cal = Calendar.getInstance();
  202. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  203. Calendar cal1 = Calendar.getInstance();
  204. cal1.setTime(cal.getTime());
  205. cal1.add(Calendar.DAY_OF_MONTH , +1);
  206. return cal1.getTimeInMillis() / 1000; //明天凌晨
  207. }
  208. /**
  209. * 获取当月第一天的凌晨时间戳
  210. */
  211. public static Long getToMonthZeroTime()
  212. {
  213. Calendar cal = Calendar.getInstance();
  214. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
  215. return cal.getTime().getTime()/1000;
  216. }
  217. /**
  218. * 根据当前日期获得所在周的日期区间(周一和周日日期)
  219. */
  220. public static Map<String, Long> getTimeInterval(Date date){
  221. Map<String, Long> map = new HashMap<>();
  222. Calendar cal = Calendar.getInstance();
  223. cal.setTime(date);
  224. // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
  225. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
  226. if(1 == dayWeek){
  227. cal.add(Calendar.DAY_OF_MONTH,-1);
  228. }
  229. // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  230. cal.setFirstDayOfWeek(Calendar.MONDAY);
  231. // 获得当前日期是一个星期的第几天
  232. int day = cal.get(Calendar.DAY_OF_WEEK);
  233. // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
  234. cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
  235. Long imptimeBegin = cal.getTime().getTime();
  236. cal.add(Calendar.DATE,6);
  237. Long imptimeEnd = cal.getTime().getTime();
  238. map.put("start", imptimeBegin/1000);
  239. map.put("end", imptimeEnd/1000);
  240. return map;
  241. }
  242. /**
  243. * 根据当前日期获得上周的日期区间(上周周一和周日日期)
  244. */
  245. public static Map<String, Long> getLastTimeInterval(Date date){
  246. Map<String, Long> map = new HashMap<>();
  247. Calendar calendar1 = Calendar.getInstance();
  248. Calendar calendar2 = Calendar.getInstance();
  249. calendar1.setTime(date);
  250. calendar2.setTime(date);
  251. int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
  252. if(dayOfWeek <= 0){
  253. dayOfWeek = 7;
  254. }
  255. int offset1 = 1 - dayOfWeek;
  256. int offset2 = 7 - dayOfWeek;
  257. calendar1.add(Calendar.DATE, offset1 - 7);
  258. calendar2.add(Calendar.DATE, offset2 - 7);
  259. // last Monday
  260. Long lastBeginDate = calendar1.getTime().getTime();
  261. // last Sunday
  262. Long lastEndDate = calendar2.getTime().getTime();
  263. map.put("laststart", lastBeginDate/1000);
  264. map.put("lastend", lastEndDate/1000);
  265. return map;
  266. }
  267. /**
  268. * 获取日期格式订单号
  269. * @return
  270. */
  271. public static String getDateOrderSn()
  272. {
  273. DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  274. String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
  275. //随机数
  276. String randomNumeric = RandomStringUtils.randomNumeric(8);
  277. return localDate+randomNumeric;
  278. }
  279. public static String getDateInputOrderSn()
  280. {
  281. return "LD"+getDateOrderSn();
  282. }
  283. public static String getPayOrderSn()
  284. {
  285. return "P"+getDateOrderSn();
  286. }
  287. public static String secToTime(int time) {
  288. String timeStr = null;
  289. int hour = 0;
  290. int minute = 0;
  291. int second = 0;
  292. if (time <= 0)
  293. return "00:00";
  294. else {
  295. minute = time / 60;
  296. if (minute < 60) {
  297. second = time % 60;
  298. timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
  299. } else {
  300. hour = minute / 60;
  301. if (hour > 99)
  302. return "99:59:59";
  303. minute = minute % 60;
  304. second = time - hour * 3600 - minute * 60;
  305. timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
  306. }
  307. }
  308. return timeStr;
  309. }
  310. public static String unitFormat(int i) {
  311. String retStr = null;
  312. if (i >= 0 && i < 10)
  313. retStr = "0" + Integer.toString(i);
  314. else
  315. retStr = "" + i;
  316. return retStr;
  317. }
  318. public static Integer durationFormat(String duration) {
  319. int index1=duration.indexOf(":");
  320. int index2=duration.indexOf(":",index1+1);
  321. int hh=Integer.parseInt(duration.substring(0,index1));
  322. int mi=Integer.parseInt(duration.substring(index1+1,index2));
  323. int ss=Integer.parseInt(duration.substring(index2+1));
  324. return hh*60*60+mi*60+ss;
  325. }
  326. }