DateUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package thyyxxk.wxservice_server.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. /**
  9. * @author dj
  10. */
  11. @Slf4j
  12. public class DateUtil {
  13. private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
  14. public static String today() {
  15. Date date = new Date();
  16. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  17. return sdf.format(date);
  18. }
  19. public static String formatDatetime(Date date) {
  20. return formatDatetime(date, DEFAULT_PATTERN);
  21. }
  22. public static String formatDatetime(Date date, String pattern) {
  23. if (null == date) {
  24. date = new Date();
  25. }
  26. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  27. return sdf.format(date);
  28. }
  29. public static Date parseDatetime(String source) {
  30. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  31. try {
  32. return df.parse(source);
  33. } catch (ParseException e) {
  34. return null;
  35. }
  36. }
  37. public static int parseHourMinuteToInteger() {
  38. SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
  39. return Integer.parseInt(sdf.format(new Date()));
  40. }
  41. public static String parseTimestamp(Long timestamp) {
  42. Date date = new Date(timestamp);
  43. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44. return df.format(date);
  45. }
  46. public static String parseTimestamp(Long timestamp, String pattern) {
  47. if (null == timestamp) {
  48. return null;
  49. }
  50. Date date = new Date(timestamp);
  51. SimpleDateFormat df = new SimpleDateFormat(pattern);
  52. return df.format(date);
  53. }
  54. public static String[] getDatesInOneWeek() {
  55. String[] result = new String[7];
  56. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  57. Calendar calendar = Calendar.getInstance();
  58. result[0] = sdf.format(new Date());
  59. for (int i = 1; i < result.length; i++) {
  60. calendar.add(Calendar.DATE, 1);
  61. result[i] = sdf.format(calendar.getTime());
  62. }
  63. return result;
  64. }
  65. public static String[] getOneWeekOffset() {
  66. String[] result = new String[2];
  67. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  68. Calendar calendar = Calendar.getInstance();
  69. result[0] = sdf.format(new Date()) + " 00:00:00";
  70. calendar.add(Calendar.DATE, 6);
  71. result[1] = sdf.format(calendar.getTime()) + " 23:59:59";
  72. return result;
  73. }
  74. public static Date getExpireDate(int effectiveDays) {
  75. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  76. Calendar calendar = Calendar.getInstance();
  77. calendar.add(Calendar.DATE, effectiveDays);
  78. return calendar.getTime();
  79. }
  80. public static boolean orderValid(Date create) {
  81. long offset = System.currentTimeMillis() - create.getTime();
  82. return (offset / 1000 / 60) < 60;
  83. }
  84. public static String formatBirthday(String socialNo, String birth) throws Exception {
  85. String day = StringUtil.isBlank(socialNo) ? birth : socialNo.substring(6, 14);
  86. SimpleDateFormat origin = new SimpleDateFormat("yyyyMMdd");
  87. SimpleDateFormat destination = new SimpleDateFormat("yyyy-MM-dd");
  88. return destination.format(origin.parse(day));
  89. }
  90. public static Integer calculateAge(String socialNo, String birth) throws Exception {
  91. String dates = StringUtil.isBlank(socialNo) ? birth : socialNo.substring(6, 14);
  92. Date nowDate = new Date();
  93. //获取当前时间
  94. DateFormat df = new SimpleDateFormat("yyyyMMdd");
  95. Date birthDate = df.parse(dates);
  96. //格式化出生日期
  97. long diff = nowDate.getTime() - birthDate.getTime();
  98. long ages = diff / (1000 * 60 * 60 * 24) / 365;
  99. return (int) ages;
  100. }
  101. public static String calculateSex(String socialNo) {
  102. socialNo = socialNo.trim();
  103. char c = socialNo.length() == 18 ? socialNo.charAt(socialNo.length() - 2) : socialNo.charAt(socialNo.length() - 1);
  104. if (StringUtil.isBlank(String.valueOf(c))) {
  105. return null;
  106. }
  107. try {
  108. int gender = Integer.parseInt(String.valueOf(c));
  109. return gender % 2 == 1 ? "M" : "F";
  110. } catch (Exception e) {
  111. log.error("无法从身份证 {} 中解析出性别。", socialNo);
  112. return null;
  113. }
  114. }
  115. private static final String[] TEMPLATE_PATTERNS = {"yyyyMMdd", "yyyy.MM.dd", "yyyy-MM-dd",
  116. "yyyy-MM-d", "yyyy-M-dd", "yyyy-M-d", "yyyyMMd", "yyyyMdd", "yyyyMd", "yyyy年MM月dd日", "yyyy年MM月d日",
  117. "yyyy年M月dd日", "yyyy年M月d日", "yyyy/MM/dd", "yyyy/MM/d", "yyyy/M/dd",
  118. "yyyy/M/d", "yyyy.MM.d", "yyyy.M.dd", "yyyy.M.d"};
  119. public static String matchDate(String date, String pattern) {
  120. if (StringUtil.isBlank(date)) {
  121. return null;
  122. }
  123. Date parse = null;
  124. boolean isFlag = false;
  125. for (String templatePattern : TEMPLATE_PATTERNS) {
  126. try {
  127. SimpleDateFormat format = new SimpleDateFormat(templatePattern);
  128. parse = format.parse(date);
  129. } catch (Exception e) {
  130. continue;
  131. }
  132. isFlag = true;
  133. break;
  134. }
  135. if (isFlag) {
  136. SimpleDateFormat formatRes = new SimpleDateFormat(pattern);
  137. return formatRes.format(parse);
  138. }
  139. return null;
  140. }
  141. }