DateUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package com.slibra.common.utils;
  2. import java.lang.management.ManagementFactory;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.time.LocalDate;
  6. import java.time.LocalDateTime;
  7. import java.time.LocalTime;
  8. import java.time.ZoneId;
  9. import java.time.ZonedDateTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.List;
  15. import org.apache.commons.lang3.time.DateFormatUtils;
  16. /**
  17. * 时间工具类
  18. *
  19. *
  20. */
  21. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  22. {
  23. public static String YYYY = "yyyy";
  24. public static String YYYY_MM = "yyyy-MM";
  25. public static String YYYY_MM_DD = "yyyy-MM-dd";
  26. public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  27. public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  28. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  29. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  30. public static String YYYYMMDDHHMM_TS = "yyyy/MM/dd HH:mm";
  31. public static String YYYYMMDDHH_TS = "yyyy/MM/dd HH";
  32. public static String YYYYMMDD_TS = "yyyy/MM/dd";
  33. public static String YYYYMMDDHH_CHINESE = "yyyy年MM月dd日 HH";
  34. private static String[] parsePatterns = {
  35. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  36. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  37. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  38. /**
  39. * 获取当前Date型日期
  40. *
  41. * @return Date() 当前日期
  42. */
  43. public static Date getNowDate()
  44. {
  45. return new Date();
  46. }
  47. /**
  48. * 获取当前日期, 默认格式为yyyy-MM-dd
  49. *
  50. * @return String
  51. */
  52. public static String getDate()
  53. {
  54. return dateTimeNow(YYYY_MM_DD);
  55. }
  56. public static final String getTime()
  57. {
  58. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  59. }
  60. public static final String dateTimeNow()
  61. {
  62. return dateTimeNow(YYYYMMDDHHMMSS);
  63. }
  64. public static final String dateTimeNow(final String format)
  65. {
  66. return parseDateToStr(format, new Date());
  67. }
  68. public static final String dateTime(final Date date)
  69. {
  70. return parseDateToStr(YYYY_MM_DD, date);
  71. }
  72. public static final String parseDateToStr(final String format, final Date date)
  73. {
  74. return new SimpleDateFormat(format).format(date);
  75. }
  76. public static final Date dateTime(final String format, final String ts)
  77. {
  78. try
  79. {
  80. return new SimpleDateFormat(format).parse(ts);
  81. }
  82. catch (ParseException e)
  83. {
  84. throw new RuntimeException(e);
  85. }
  86. }
  87. /**
  88. * 日期路径 即年/月/日 如2018/08/08
  89. */
  90. public static final String datePath()
  91. {
  92. Date now = new Date();
  93. return DateFormatUtils.format(now, "yyyy/MM/dd");
  94. }
  95. /**
  96. * 日期路径 即年/月/日 如20180808
  97. */
  98. public static final String dateTime()
  99. {
  100. Date now = new Date();
  101. return DateFormatUtils.format(now, "yyyyMMdd");
  102. }
  103. /**
  104. * 日期型字符串转化为日期 格式
  105. */
  106. public static Date parseDate(Object str)
  107. {
  108. if (str == null)
  109. {
  110. return null;
  111. }
  112. try
  113. {
  114. return parseDate(str.toString(), parsePatterns);
  115. }
  116. catch (ParseException e)
  117. {
  118. return null;
  119. }
  120. }
  121. /**
  122. * 获取服务器启动时间
  123. */
  124. public static Date getServerStartDate()
  125. {
  126. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  127. return new Date(time);
  128. }
  129. /**
  130. * 计算相差天数
  131. */
  132. public static int differentDaysByMillisecond(Date date1, Date date2)
  133. {
  134. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  135. }
  136. /**
  137. * 计算相差小时数
  138. */
  139. public static int differentHoursByMillisecond(Date date1, Date date2)
  140. {
  141. return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600)));
  142. }
  143. /**
  144. * 计算时间差
  145. *
  146. * @param endDate 最后时间
  147. * @param startTime 开始时间
  148. * @return 时间差(天/小时/分钟)
  149. */
  150. public static String timeDistance(Date endDate, Date startTime)
  151. {
  152. long nd = 1000 * 24 * 60 * 60;
  153. long nh = 1000 * 60 * 60;
  154. long nm = 1000 * 60;
  155. // long ns = 1000;
  156. // 获得两个时间的毫秒时间差异
  157. long diff = endDate.getTime() - startTime.getTime();
  158. // 计算差多少天
  159. long day = diff / nd;
  160. // 计算差多少小时
  161. long hour = diff % nd / nh;
  162. // 计算差多少分钟
  163. long min = diff % nd % nh / nm;
  164. // 计算差多少秒//输出结果
  165. // long sec = diff % nd % nh % nm / ns;
  166. return day + "天" + hour + "小时" + min + "分钟";
  167. }
  168. /**
  169. * 增加 LocalDateTime ==> Date
  170. */
  171. public static Date toDate(LocalDateTime temporalAccessor)
  172. {
  173. ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  174. return Date.from(zdt.toInstant());
  175. }
  176. /**
  177. * 增加 LocalDate ==> Date
  178. */
  179. public static Date toDate(LocalDate temporalAccessor)
  180. {
  181. LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  182. ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  183. return Date.from(zdt.toInstant());
  184. }
  185. public static Date plusDate(int day, Date current)
  186. {
  187. // 创建一个Calendar实例,并将日期设置为今天
  188. Calendar calendar = Calendar.getInstance();
  189. calendar.setTime(current);
  190. // 将日期设置为前一天
  191. calendar.add(Calendar.DATE, day);
  192. // 转换
  193. return calendar.getTime();
  194. }
  195. public static List<String> getAllDatesBetween(LocalDate startDate, LocalDate endDate) {
  196. List<String> dates = new ArrayList<>();
  197. LocalDate currentDate = startDate;
  198. while (!currentDate.isAfter(endDate)) {
  199. dates.add(currentDate.format(DateTimeFormatter.ofPattern(DateUtils.YYYYMMDD_TS)));
  200. currentDate = currentDate.plusDays(1);
  201. }
  202. return dates;
  203. }
  204. public static void main(String[] args) {
  205. Date nowDate = DateUtils.getNowDate();
  206. Date date = plusDate(1, nowDate);
  207. Date date1 = plusDate(-1, nowDate);
  208. System.out.println(date);
  209. System.out.println(date1);
  210. }
  211. }