DateUtils.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.pavis.ctr.audit.common.utils;
  2. import org.apache.commons.lang3.time.DateFormatUtils;
  3. import java.lang.management.ManagementFactory;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.time.*;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. /**
  10. * 时间工具类
  11. *
  12. * @author alibct
  13. */
  14. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  15. public static String YYYY = "yyyy";
  16. public static String YYYY_MM = "yyyy-MM";
  17. public static String YYYY_MM_DD = "yyyy-MM-dd";
  18. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  19. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  20. private static String[] parsePatterns = {
  21. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  22. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  23. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  24. /**
  25. * 获取当前Date型日期
  26. *
  27. * @return Date() 当前日期
  28. */
  29. public static Date getNowDate() {
  30. return new Date();
  31. }
  32. /**
  33. * 获取当前日期, 默认格式为yyyy-MM-dd
  34. *
  35. * @return String
  36. */
  37. public static String getDate() {
  38. return dateTimeNow(YYYY_MM_DD);
  39. }
  40. public static final String getTime() {
  41. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  42. }
  43. public static final String dateTimeNow() {
  44. return dateTimeNow(YYYYMMDDHHMMSS);
  45. }
  46. public static final String dateTimeNow(final String format) {
  47. return parseDateToStr(format, new Date());
  48. }
  49. public static final String dateTime(final Date date) {
  50. return parseDateToStr(YYYY_MM_DD, date);
  51. }
  52. public static final String parseDateToStr(final String format, final Date date) {
  53. return new SimpleDateFormat(format).format(date);
  54. }
  55. public static final Date dateTime(final String format, final String ts) {
  56. try {
  57. return new SimpleDateFormat(format).parse(ts);
  58. } catch (ParseException e) {
  59. throw new RuntimeException(e);
  60. }
  61. }
  62. /**
  63. * 日期路径 即年/月/日 如2018/08/08
  64. */
  65. public static final String datePath() {
  66. Date now = new Date();
  67. return DateFormatUtils.format(now, "yyyy/MM/dd");
  68. }
  69. /**
  70. * 日期路径 即年/月/日 如20180808
  71. */
  72. public static final String dateTime() {
  73. Date now = new Date();
  74. return DateFormatUtils.format(now, "yyyyMMdd");
  75. }
  76. /**
  77. * 日期型字符串转化为日期 格式
  78. */
  79. public static Date parseDate(Object str) {
  80. if (str == null) {
  81. return null;
  82. }
  83. try {
  84. return parseDate(str.toString(), parsePatterns);
  85. } catch (ParseException e) {
  86. return null;
  87. }
  88. }
  89. /**
  90. * 获取服务器启动时间
  91. */
  92. public static Date getServerStartDate() {
  93. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  94. return new Date(time);
  95. }
  96. /**
  97. * 计算相差天数
  98. */
  99. public static int differentDaysByMillisecond(Date date1, Date date2) {
  100. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  101. }
  102. /**
  103. * 计算时间差
  104. *
  105. * @param endDate 最后时间
  106. * @param startTime 开始时间
  107. * @return 时间差(天/小时/分钟)
  108. */
  109. public static String timeDistance(Date endDate, Date startTime) {
  110. long nd = 1000 * 24 * 60 * 60;
  111. long nh = 1000 * 60 * 60;
  112. long nm = 1000 * 60;
  113. // long ns = 1000;
  114. // 获得两个时间的毫秒时间差异
  115. long diff = endDate.getTime() - startTime.getTime();
  116. // 计算差多少天
  117. long day = diff / nd;
  118. // 计算差多少小时
  119. long hour = diff % nd / nh;
  120. // 计算差多少分钟
  121. long min = diff % nd % nh / nm;
  122. // 计算差多少秒//输出结果
  123. // long sec = diff % nd % nh % nm / ns;
  124. return day + "天" + hour + "小时" + min + "分钟";
  125. }
  126. /**
  127. * 增加 LocalDateTime ==> Date
  128. */
  129. public static Date toDate(LocalDateTime temporalAccessor) {
  130. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  131. return Date.from(zdt.toInstant());
  132. }
  133. /**
  134. * 增加 LocalDate ==> Date
  135. */
  136. public static Date toDate(LocalDate temporalAccessor) {
  137. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  138. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  139. return Date.from(zdt.toInstant());
  140. }
  141. /**
  142. * 当前时间往后减一天 接受时间类型:yyyy-mm-dd的字符串
  143. *
  144. * @return
  145. */
  146. public static String minusOneDay(String time) throws ParseException {
  147. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
  148. //将接收的time中的年月日截取成String数组
  149. String[] timeStr = time.split("-");
  150. Calendar calendar = Calendar.getInstance();
  151. int year = Integer.valueOf(timeStr[0]);
  152. int month = Integer.valueOf(timeStr[1]);
  153. int day = Integer.valueOf(timeStr[2]);
  154. //判断time中的日期是否是该年该月的一号,如果不是就往前减一天;如果是就看情况减月份和年份
  155. if (day <= 1) {
  156. String date = null;
  157. //如果月份不是1月,就对月份减一;如果月份是1月,就对年份减一;
  158. if (month > 1) {
  159. month--;
  160. Calendar c = Calendar.getInstance();
  161. c.set(year, month, 0);
  162. Date parse = dateFormat.parse(year + "-" + month + "-" + c.get(c.DAY_OF_MONTH));
  163. date = dateFormat.format(parse);
  164. } else if (month == 1) {
  165. year--;
  166. date = year + "-12-31";
  167. }
  168. return date;
  169. }
  170. //time中的日期不是该年该月的一号,直接往前减一天
  171. Date date = dateFormat.parse(time);
  172. calendar.setTime(date);
  173. calendar.add(calendar.DATE, -1);
  174. return dateFormat.format(calendar.getTime());
  175. }
  176. /**
  177. * 当前时间往前加一天 接受时间类型:yyyy-mm-dd的字符串
  178. *
  179. * @return
  180. */
  181. public static String addOneDay(String time) throws ParseException {
  182. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
  183. //将接收的time中的年月日截取成String数组
  184. String[] timeStr = time.split("-");
  185. //确定time中的那一年的那个月份有多少天
  186. Calendar calendar = Calendar.getInstance();
  187. int year = Integer.valueOf(timeStr[0]);
  188. int month = Integer.valueOf(timeStr[1]);
  189. int day = Integer.valueOf(timeStr[2]);
  190. calendar.set(year, month, 0);
  191. int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
  192. //判断time中的日期是否超过了该年该月的最后一天,如果超过就进行以下处理
  193. if (day >= dayOfMonth) {
  194. String date = null;
  195. //判断月份是否是12月,不是就往后加一个月;是的话就把年份加一年
  196. if (month < 12) {
  197. month++;
  198. Date parse = dateFormat.parse(year + "-" + month + "-01");
  199. date = dateFormat.format(parse);
  200. } else if (month == 12) {
  201. year++;
  202. date = year + "-01-01";
  203. }
  204. return date;
  205. }
  206. //time中的日期没有超过该年该月的最后一天,则天数往后加一天
  207. calendar.setTime(dateFormat.parse(time));
  208. calendar.add(calendar.DATE, 1);
  209. return dateFormat.format(calendar.getTime());
  210. }
  211. }