DateUtil.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package cn.com.qmth.examcloud.commons.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
  7. /**
  8. * 日期工具
  9. *
  10. * @author WANGWEI
  11. */
  12. public class DateUtil {
  13. /**
  14. * patterns describing the date and time format
  15. *
  16. * @author WANGWEI
  17. */
  18. public interface DatePatterns {
  19. public static final String DEFAULT = "yyyyMMddHHmmss";
  20. public static final String YYYY = "yyyy";
  21. public static final String YYYYMM = "yyyyMM";
  22. public static final String YYYYMMDD = "yyyyMMdd";
  23. public static final String YYYYMMDDHH = "yyyyMMddHH";
  24. public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
  25. public static final String CHINA_DEFAULT = "yyyy-MM-dd HH:mm:ss";
  26. public static final String YYYY_MM = "yyyy-MM";
  27. public static final String YYYY_MM_DD = "yyyy-MM-dd";
  28. public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  29. public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
  30. }
  31. /**
  32. * get now date.
  33. *
  34. * @param pattern
  35. * @return
  36. */
  37. public static String now(String pattern) {
  38. return format(new Date(), pattern);
  39. }
  40. /**
  41. * get now china date.
  42. *
  43. * @return
  44. */
  45. public static String chinaNow() {
  46. return format(new Date(), DatePatterns.CHINA_DEFAULT);
  47. }
  48. /**
  49. * format date.
  50. *
  51. * @param date
  52. * @param pattern
  53. * @return
  54. */
  55. public static String format(Date date, String pattern) {
  56. try {
  57. SimpleDateFormat df = new SimpleDateFormat(pattern);
  58. return df.format(date);
  59. } catch (Exception e) {
  60. throw new ExamCloudRuntimeException(e);
  61. }
  62. }
  63. /**
  64. * format now date.
  65. *
  66. * @author WANGWEI
  67. * @param pattern
  68. * @return
  69. */
  70. public static String formatNow(String pattern) {
  71. try {
  72. SimpleDateFormat df = new SimpleDateFormat(pattern);
  73. return df.format(new Date());
  74. } catch (Exception e) {
  75. throw new ExamCloudRuntimeException(e);
  76. }
  77. }
  78. /**
  79. * parse date.
  80. *
  81. * @param source
  82. * @param pattern
  83. * @return
  84. */
  85. public static Date parse(String source, String pattern) {
  86. SimpleDateFormat df = new SimpleDateFormat(pattern);
  87. try {
  88. return df.parse(source);
  89. } catch (ParseException e) {
  90. throw new ExamCloudRuntimeException(e);
  91. }
  92. }
  93. /**
  94. * 是否同一天
  95. *
  96. * @author WANGWEI
  97. * @param date1
  98. * @param date2
  99. * @return
  100. */
  101. public static boolean isSameDay(Date date1, Date date2) {
  102. if (null == date1) {
  103. throw new ExamCloudRuntimeException("first argument must not be null");
  104. }
  105. if (null == date2) {
  106. throw new ExamCloudRuntimeException("second argument must not be null");
  107. }
  108. Calendar cal1 = Calendar.getInstance();
  109. cal1.setTime(date1);
  110. Calendar cal2 = Calendar.getInstance();
  111. cal2.setTime(date2);
  112. return isSameDay(cal1, cal2);
  113. }
  114. /**
  115. * 是否同一天
  116. *
  117. * @author WANGWEI
  118. * @param calendar1
  119. * @param calendar2
  120. * @return
  121. */
  122. public static boolean isSameDay(Calendar calendar1, Calendar calendar2) {
  123. if (null == calendar1) {
  124. throw new ExamCloudRuntimeException("first argument must not be null");
  125. }
  126. if (null == calendar2) {
  127. throw new ExamCloudRuntimeException("second argument must not be null");
  128. }
  129. return calendar1.get(0) == calendar2.get(0) && calendar1.get(1) == calendar2.get(1)
  130. && calendar1.get(6) == calendar2.get(6);
  131. }
  132. }