DateUtils.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.mokamrp.privates.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.util.ArrayList;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.List;
  10. /**
  11. * 时间工具类
  12. *
  13. * @author ruoyi
  14. */
  15. public class DateUtils extends org.apache.commons.lang3.time.DateUtils
  16. {
  17. public static String YYYY = "yyyy";
  18. public static String YYYY_MM = "yyyy-MM";
  19. public static String YYYY_MM_DD = "yyyy-MM-dd";
  20. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  21. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  22. private static String[] parsePatterns = {
  23. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  24. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  25. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  26. /**
  27. * 获取当前Date型日期
  28. *
  29. * @return Date() 当前日期
  30. */
  31. public static Date getNowDate()
  32. {
  33. return new Date();
  34. }
  35. /**
  36. * 获取当前日期, 默认格式为yyyy-MM-dd
  37. *
  38. * @return String
  39. */
  40. public static String getDate()
  41. {
  42. return dateTimeNow(YYYY_MM_DD);
  43. }
  44. public static final String getTime()
  45. {
  46. return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
  47. }
  48. public static final String dateTimeNow()
  49. {
  50. return dateTimeNow(YYYYMMDDHHMMSS);
  51. }
  52. public static final String dateTimeNow(final String format)
  53. {
  54. return parseDateToStr(format, new Date());
  55. }
  56. public static final String dateTime(final Date date)
  57. {
  58. return parseDateToStr(YYYY_MM_DD, date);
  59. }
  60. public static final String parseDateToStr(final String format, final Date date)
  61. {
  62. return new SimpleDateFormat(format).format(date);
  63. }
  64. public static final Date dateTime(final String format, final String ts)
  65. {
  66. try
  67. {
  68. return new SimpleDateFormat(format).parse(ts);
  69. }
  70. catch (ParseException e)
  71. {
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. /**
  76. * 日期路径 即年/月/日 如2018/08/08
  77. */
  78. public static final String datePath()
  79. {
  80. Date now = new Date();
  81. return DateFormatUtils.format(now, "yyyy/MM/dd");
  82. }
  83. /**
  84. * 日期路径 即年/月/日 如20180808
  85. */
  86. public static final String dateTime()
  87. {
  88. Date now = new Date();
  89. return DateFormatUtils.format(now, "yyyyMMdd");
  90. }
  91. /**
  92. * 日期型字符串转化为日期 格式
  93. */
  94. public static Date parseDate(Object str)
  95. {
  96. if (str == null)
  97. {
  98. return null;
  99. }
  100. try
  101. {
  102. return parseDate(str.toString(), parsePatterns);
  103. }
  104. catch (ParseException e)
  105. {
  106. return null;
  107. }
  108. }
  109. /**
  110. * 获取服务器启动时间
  111. */
  112. public static Date getServerStartDate()
  113. {
  114. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  115. return new Date(time);
  116. }
  117. /**
  118. * 计算两个时间差
  119. */
  120. public static String getDatePoor(Date endDate, Date nowDate)
  121. {
  122. long nd = 1000 * 24 * 60 * 60;
  123. long nh = 1000 * 60 * 60;
  124. long nm = 1000 * 60;
  125. // long ns = 1000;
  126. // 获得两个时间的毫秒时间差异
  127. long diff = endDate.getTime() - nowDate.getTime();
  128. // 计算差多少天
  129. long day = diff / nd;
  130. // 计算差多少小时
  131. long hour = diff % nd / nh;
  132. // 计算差多少分钟
  133. long min = diff % nd % nh / nm;
  134. // 计算差多少秒//输出结果
  135. // long sec = diff % nd % nh % nm / ns;
  136. return day + "天" + hour + "小时" + min + "分钟";
  137. }
  138. /**
  139. * 计算两个时间差
  140. */
  141. public static long diffTime(Date endDate, Date nowDate){
  142. return endDate.getTime() - nowDate.getTime();
  143. }
  144. /**
  145. * 获取时间段内所有日期
  146. * @param dBegin
  147. * @param dEnd
  148. * @return
  149. */
  150. public static List<Date> findDates(Date dBegin, Date dEnd)
  151. {
  152. List lDate = new ArrayList();
  153. lDate.add(dBegin);
  154. Calendar calBegin = Calendar.getInstance();
  155. // 使用给定的 Date 设置此 Calendar 的时间
  156. calBegin.setTime(dBegin);
  157. Calendar calEnd = Calendar.getInstance();
  158. // 使用给定的 Date 设置此 Calendar 的时间
  159. calEnd.setTime(dEnd);
  160. // 测试此日期是否在指定日期之后
  161. while (dEnd.after(calBegin.getTime()))
  162. {
  163. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  164. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  165. lDate.add(calBegin.getTime());
  166. }
  167. return lDate;
  168. }
  169. public static long getMillionSceondsBydate(String date) throws ParseException {
  170. SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
  171. long millionSeconds = sdf.parse(date).getTime();//毫秒
  172. return millionSeconds;
  173. }
  174. public static int getAge(Date birthDay) throws Exception {
  175. Calendar cal = Calendar.getInstance();
  176. if (cal.before(birthDay)) { //出生日期晚于当前时间,无法计算
  177. throw new IllegalArgumentException(
  178. "The birthDay is before Now.It's unbelievable!");
  179. }
  180. int yearNow = cal.get(Calendar.YEAR); //当前年份
  181. int monthNow = cal.get(Calendar.MONTH); //当前月份
  182. int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
  183. cal.setTime(birthDay);
  184. int yearBirth = cal.get(Calendar.YEAR);
  185. int monthBirth = cal.get(Calendar.MONTH);
  186. int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
  187. int age = yearNow - yearBirth; //计算整岁数
  188. if (monthNow <= monthBirth) {
  189. if (monthNow == monthBirth) {
  190. if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一
  191. }else{
  192. age--;//当前月份在生日之前,年龄减一
  193. } }
  194. return age;
  195. }
  196. }