123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package thyyxxk.wxservice_server.utils;
- import lombok.extern.slf4j.Slf4j;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * @author dj
- */
- @Slf4j
- public class DateUtil {
- private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
- public static String today() {
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(date);
- }
- public static String formatDatetime(Date date) {
- return formatDatetime(date, DEFAULT_PATTERN);
- }
- public static String formatDatetime(Date date, String pattern) {
- if (null == date) {
- date = new Date();
- }
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- return sdf.format(date);
- }
- public static Date parseDatetime(String source) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- try {
- return df.parse(source);
- } catch (ParseException e) {
- return null;
- }
- }
- public static int parseHourMinuteToInteger() {
- SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
- return Integer.parseInt(sdf.format(new Date()));
- }
- public static String parseTimestamp(Long timestamp) {
- Date date = new Date(timestamp);
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return df.format(date);
- }
- public static String parseTimestamp(Long timestamp, String pattern) {
- if (null == timestamp) {
- return null;
- }
- Date date = new Date(timestamp);
- SimpleDateFormat df = new SimpleDateFormat(pattern);
- return df.format(date);
- }
- public static String[] getDatesInOneWeek() {
- String[] result = new String[7];
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar calendar = Calendar.getInstance();
- result[0] = sdf.format(new Date());
- for (int i = 1; i < result.length; i++) {
- calendar.add(Calendar.DATE, 1);
- result[i] = sdf.format(calendar.getTime());
- }
- return result;
- }
- public static String[] getOneWeekOffset() {
- String[] result = new String[2];
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar calendar = Calendar.getInstance();
- result[0] = sdf.format(new Date()) + " 00:00:00";
- calendar.add(Calendar.DATE, 6);
- result[1] = sdf.format(calendar.getTime()) + " 23:59:59";
- return result;
- }
- public static Date getExpireDate(int effectiveDays) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DATE, effectiveDays);
- return calendar.getTime();
- }
- public static boolean orderValid(Date create) {
- long offset = System.currentTimeMillis() - create.getTime();
- return (offset / 1000 / 60) < 60;
- }
- public static String formatBirthday(String socialNo, String birth) throws Exception {
- String day = StringUtil.isBlank(socialNo) ? birth : socialNo.substring(6, 14);
- SimpleDateFormat origin = new SimpleDateFormat("yyyyMMdd");
- SimpleDateFormat destination = new SimpleDateFormat("yyyy-MM-dd");
- return destination.format(origin.parse(day));
- }
- public static Integer calculateAge(String socialNo, String birth) throws Exception {
- String dates = StringUtil.isBlank(socialNo) ? birth : socialNo.substring(6, 14);
- Date nowDate = new Date();
- //获取当前时间
- DateFormat df = new SimpleDateFormat("yyyyMMdd");
- Date birthDate = df.parse(dates);
- //格式化出生日期
- long diff = nowDate.getTime() - birthDate.getTime();
- long ages = diff / (1000 * 60 * 60 * 24) / 365;
- return (int) ages;
- }
- public static String calculateSex(String socialNo) {
- socialNo = socialNo.trim();
- char c = socialNo.length() == 18 ? socialNo.charAt(socialNo.length() - 2) : socialNo.charAt(socialNo.length() - 1);
- if (StringUtil.isBlank(String.valueOf(c))) {
- return null;
- }
- try {
- int gender = Integer.parseInt(String.valueOf(c));
- return gender % 2 == 1 ? "M" : "F";
- } catch (Exception e) {
- log.error("无法从身份证 {} 中解析出性别。", socialNo);
- return null;
- }
- }
- private static final String[] TEMPLATE_PATTERNS = {"yyyyMMdd", "yyyy.MM.dd", "yyyy-MM-dd",
- "yyyy-MM-d", "yyyy-M-dd", "yyyy-M-d", "yyyyMMd", "yyyyMdd", "yyyyMd", "yyyy年MM月dd日", "yyyy年MM月d日",
- "yyyy年M月dd日", "yyyy年M月d日", "yyyy/MM/dd", "yyyy/MM/d", "yyyy/M/dd",
- "yyyy/M/d", "yyyy.MM.d", "yyyy.M.dd", "yyyy.M.d"};
- public static String matchDate(String date, String pattern) {
- if (StringUtil.isBlank(date)) {
- return null;
- }
- Date parse = null;
- boolean isFlag = false;
- for (String templatePattern : TEMPLATE_PATTERNS) {
- try {
- SimpleDateFormat format = new SimpleDateFormat(templatePattern);
- parse = format.parse(date);
- } catch (Exception e) {
- continue;
- }
- isFlag = true;
- break;
- }
- if (isFlag) {
- SimpleDateFormat formatRes = new SimpleDateFormat(pattern);
- return formatRes.format(parse);
- }
- return null;
- }
- }
|