DateUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package cn.com.qmth.examcloud.commons.util;
  2. import java.math.BigDecimal;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
  8. /**
  9. * 日期工具
  10. *
  11. * @author WANGWEI
  12. */
  13. public class DateUtil {
  14. /**
  15. * patterns describing the date and time format
  16. *
  17. * @author WANGWEI
  18. */
  19. public interface DatePatterns {
  20. public static final String DEFAULT = "yyyyMMddHHmmss";
  21. public static final String YYYY = "yyyy";
  22. public static final String YYYYMM = "yyyyMM";
  23. public static final String YYYYMMDD = "yyyyMMdd";
  24. public static final String YYYYMMDDHH = "yyyyMMddHH";
  25. public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
  26. public static final String CHINA_DEFAULT = "yyyy-MM-dd HH:mm:ss";
  27. public static final String YYYY_MM = "yyyy-MM";
  28. public static final String YYYY_MM_DD = "yyyy-MM-dd";
  29. public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  30. public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
  31. }
  32. /**
  33. * get now date.
  34. *
  35. * @param pattern
  36. * @return
  37. */
  38. public static String now(String pattern) {
  39. return format(new Date(), pattern);
  40. }
  41. /**
  42. * get now china date.
  43. *
  44. * @return
  45. */
  46. public static String chinaNow() {
  47. return format(new Date(), DatePatterns.CHINA_DEFAULT);
  48. }
  49. /**
  50. * format date.
  51. *
  52. * @param date
  53. * @param pattern
  54. * @return
  55. */
  56. public static String format(Date date, String pattern) {
  57. try {
  58. SimpleDateFormat df = new SimpleDateFormat(pattern);
  59. return df.format(date);
  60. } catch (Exception e) {
  61. throw new ExamCloudRuntimeException(e);
  62. }
  63. }
  64. /**
  65. * format now date.
  66. *
  67. * @author WANGWEI
  68. * @param pattern
  69. * @return
  70. */
  71. public static String formatNow(String pattern) {
  72. try {
  73. SimpleDateFormat df = new SimpleDateFormat(pattern);
  74. return df.format(new Date());
  75. } catch (Exception e) {
  76. throw new ExamCloudRuntimeException(e);
  77. }
  78. }
  79. /**
  80. * parse date.
  81. *
  82. * @param source
  83. * @param pattern
  84. * @return
  85. */
  86. public static Date parse(String source, String pattern) {
  87. SimpleDateFormat df = new SimpleDateFormat(pattern);
  88. try {
  89. return df.parse(source);
  90. } catch (ParseException e) {
  91. throw new ExamCloudRuntimeException(e);
  92. }
  93. }
  94. /**
  95. * parse date randomly.
  96. *
  97. * @author WANGWEI
  98. * @param s
  99. * @return
  100. */
  101. public static Date parseRandomly(String s) {
  102. if (s.matches("\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
  103. return parse(s, "yyyy/MM/dd HH:mm:ss");
  104. } else if (s.matches("\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}")) {
  105. return parse(s, "yyyy/MM/dd HH:mm");
  106. } else if (s.matches("\\d{4}/\\d{1,2}/\\d{1,2}")) {
  107. return parse(s, "yyyy/MM/dd");
  108. } else if (s.matches("\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
  109. return parse(s, "yyyy-MM-dd HH:mm:ss");
  110. } else if (s.matches("\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}")) {
  111. return parse(s, "yyyy-MM-dd HH:mm");
  112. } else if (s.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {
  113. return parse(s, "yyyy-MM-dd");
  114. } else {
  115. throw new ExamCloudRuntimeException("unsupported date string.");
  116. }
  117. }
  118. /**
  119. * 解析excel日期
  120. *
  121. * @author WANGWEI
  122. * @param number
  123. * @return
  124. */
  125. public static Date parseExcel(String number) {
  126. BigDecimal bd = new BigDecimal(number);
  127. int days = bd.intValue();
  128. int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600);
  129. Calendar c = Calendar.getInstance();
  130. c.set(1900, 0, 1);
  131. c.add(Calendar.DATE, days - 2);
  132. int hour = mills / 3600;
  133. int minute = (mills - hour * 3600) / 60;
  134. int second = mills - hour * 3600 - minute * 60;
  135. c.set(Calendar.HOUR_OF_DAY, hour);
  136. c.set(Calendar.MINUTE, minute);
  137. c.set(Calendar.SECOND, second);
  138. Date date = c.getTime();
  139. return date;
  140. }
  141. /**
  142. * 是否同一天
  143. *
  144. * @author WANGWEI
  145. * @param date1
  146. * @param date2
  147. * @return
  148. */
  149. public static boolean isSameDay(Date date1, Date date2) {
  150. if (null == date1) {
  151. throw new ExamCloudRuntimeException("first argument must not be null");
  152. }
  153. if (null == date2) {
  154. throw new ExamCloudRuntimeException("second argument must not be null");
  155. }
  156. Calendar cal1 = Calendar.getInstance();
  157. cal1.setTime(date1);
  158. Calendar cal2 = Calendar.getInstance();
  159. cal2.setTime(date2);
  160. return isSameDay(cal1, cal2);
  161. }
  162. /**
  163. * 是否同一天
  164. *
  165. * @author WANGWEI
  166. * @param calendar1
  167. * @param calendar2
  168. * @return
  169. */
  170. public static boolean isSameDay(Calendar calendar1, Calendar calendar2) {
  171. if (null == calendar1) {
  172. throw new ExamCloudRuntimeException("first argument must not be null");
  173. }
  174. if (null == calendar2) {
  175. throw new ExamCloudRuntimeException("second argument must not be null");
  176. }
  177. return calendar1.get(0) == calendar2.get(0) && calendar1.get(1) == calendar2.get(1)
  178. && calendar1.get(6) == calendar2.get(6);
  179. }
  180. }