DateAutoHelper.java 4.39 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3 4 5 6 7 8 9 10
package com.yanzuoguang.util.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 将时间格式字符串转换为时间
yanzg's avatar
yanzg committed
11
 * @author 颜佐光
yanzg's avatar
yanzg committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
 */
public class DateAutoHelper {

    /**
     * 无符号正则表达式
     */
    public static final String DATE_FORMAT_NO_SPLIT_REGEX = "^(\\d{4})(\\d{2})(\\d{2})$";
    /**
     * 有符号正常日期格式
     */
    public static final String DATE_FORMAT_COMMON_REGEX = "^(\\d{4,})[/-](\\d{1,2})[/-](\\d{1,2})$";
    /**
     * 有符号正常日期格式替换
     */
    public static final String DATE_FORMAT_COMMON_REPLACE = "$1-$2-$3 00:00:00.000";
    /**
     * 倒序的日期格式
     */
    public static final String DATE_FORMAT_REVERT_REGEX = "^(\\d{1,2})[/-](\\d{1,2})[/-](\\d{4,})$";
    /**
     * 有符号正常日期格式替换
     */
    public static final String DATE_FORMAT_REVERT_REPLACE = "$3-$2-$1 00:00:00.000";
    /**
     * 正常时间格式
     */
    public static final String DATETIME_HOUR_FORMAT_REGEX = "^(\\d{4,})[/-](\\d{1,2})[/-](\\d{1,2}).{1}(\\d{1,2}):(\\d{1,2})$";
    /**
     * 正常时间格式替换
     */
    public static final String DATETIME_HOUR_FORMAT_REPLACE = "$1-$2-$3 $4:$5:00.000";
    /**
     * 正常时间格式
     */
    public static final String DATETIME_FORMAT_REGEX = "^(\\d{4,})[/-](\\d{1,2})[/-](\\d{1,2}).{1}(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$";
    /**
     * 正常时间格式替换
     */
    public static final String DATETIME_FORMAT_REPLACE = "$1-$2-$3 $4:$5:$6.000";
    /**
     * 时间格式化字符串 yyyy-MM-dd HH:mm:ss.SSS
     */
    public static final String DATETIME_FULL_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 缓存的自动识别的格式正则表达式
     */
    private static List<DateReplace> autoDateCache = new ArrayList<>();

    static {
        registerAutoFormat(DATE_FORMAT_NO_SPLIT_REGEX, DATE_FORMAT_COMMON_REPLACE);
        registerAutoFormat(DATE_FORMAT_COMMON_REGEX, DATE_FORMAT_COMMON_REPLACE);
        registerAutoFormat(DATE_FORMAT_REVERT_REGEX, DATE_FORMAT_REVERT_REPLACE);
        registerAutoFormat(DATETIME_HOUR_FORMAT_REGEX, DATETIME_HOUR_FORMAT_REPLACE);
        registerAutoFormat(DATETIME_FORMAT_REGEX, DATETIME_FORMAT_REPLACE);
    }

    /**
     * 时间格式字符串
     */
    private static class DateReplace {
yanzg's avatar
yanzg committed
73 74 75
        /**
         * 正则表达式
         */
yanzg's avatar
yanzg committed
76
        public String regex;
yanzg's avatar
yanzg committed
77 78 79
        /**
         * 替换表达式
         */
yanzg's avatar
yanzg committed
80
        public String replace;
yanzg's avatar
yanzg committed
81 82 83
        /**
         * 终止标志位
         */
yanzg's avatar
yanzg committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        public boolean end;
    }

    /**
     * 注册正则表达式,将时间转换为正确格式的正则表达式,后注册的会优先执行 。
     *
     * @param regex   正则表达式
     * @param replace 替换表达式
     */
    public static void registerAutoFormat(String regex, String replace) {
        registerAutoFormat(regex, replace, true);
    }

    /**
     * 注册正则表达式,将时间转换为正确格式的正则表达式,后注册的会优先执行 。
     *
     * @param regex   正则表达式
     * @param replace 替换表达式
     * @param end     是否需要结束
     */
    public static void registerAutoFormat(String regex, String replace, boolean end) {
        DateReplace item = new DateReplace();
        item.regex = regex;
        item.replace = replace;
        item.end = end;
        autoDateCache.add(item);
    }

    /**
     * 根据时间字符串自动识别时间
     *
     * @param date 时间字符串
     * @return 时间
     */
    public static Date getAutoDate(String date) throws ParseException {
        if (date == null || date.trim().length() < 1) {
            return null;
        }
        date = date.trim();
        int size = autoDateCache.size();
        for (int i = size - 1; i >= 0; i--) {
            // 遍历所有时间格式
            DateReplace item = autoDateCache.get(i);
            String dateTo = date.replaceAll(item.regex, item.replace);
            // 如何替换成功,且终止标志位为true则终止执行
            boolean isBreak = item.end && !dateTo.equals(date);
            date = dateTo;
            if (isBreak) {
                break;
            }
        }
        // 将正常格式的时间字符串转换为时间
        return new SimpleDateFormat(DATETIME_FULL_FORMAT).parse(String.valueOf(date));
    }

}