日期正则表达式

校验yyyy年MM月dd日yyyy年MM月dd日 HH:mm:ssyyyy年M月d日yyyy年M月d日 HH:mm:ss

(^([1-9]\d{3})(([0]{0,1}[1-9])|([1][0-2]))([0-3]{0,1}[0-9])((\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?))

校验yyyy-MM-ddyyyy-MM-dd HH:mm:ssyyyy-M-dyyyy-M-d HH:mm:ss

(^([1-9]\d{3}-)(([0]{0,1}[1-9]-)|([1][0-2]-))([0-3]{0,1}[0-9])((\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?))

校验yyyy/MM/ddyyyy/MM/dd HH:mm:ssyyyy/M/dyyyy/M/d HH:mm:ss

(^([1-9]\d{3}/)(([0]{0,1}[1-9]/)|([1][0-2]/))([0-3]{0,1}[0-9])((\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?))

校验yyyMMddyyyyMMddHHmmss

^([1-9]\d{3})(([0]{0,1}[1-9])|([1][0-2])){2}([0-3]{0,1}[0-9])((([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9]))?)

yyyy年MM月dd日yyyy年MM月dd日 HH:mm:ssyyyy年M月d日yyyy年M月d日 HH:mm:ssyyyy-MM-ddyyyy-MM-dd HH:mm:ssyyyy-M-dyyyy-M-d HH:mm:ssyyyy/MM/ddyyyy/MM/dd HH:mm:ssyyyy/M/dyyyy/M/d HH:mm:ssyyyMMddyyyyMMddHHmmss字符串(基本涵盖日常开发中所有的日期格式)进行日期格式化(可直接转LocalDateTime类型)

private static final Pattern DATE_PATTRN = Pattern.compile("(^([1-9]\\d{3}年)(([0]{0,1}[1-9]月)|([1][0-2]月))([0-3]{0,1}[0-9]日)((\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?))|(^([1-9]\\d{3}-)(([0]{0,1}[1-9]-)|([1][0-2]-))([0-3]{0,1}[0-9])((\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?))|(^([1-9]\\d{3}/)(([0]{0,1}[1-9]/)|([1][0-2]/))([0-3]{0,1}[0-9])((\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?))$");
private static final Pattern NUMBER_DATE_PATTRN = Pattern.compile("(^([1-9]\\d{3})(([0]{0,1}[1-9])|([1][0-2]))([0-3]{0,1}[0-9])((([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9]))?))$");  
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

private static String formatDate(Object value){
        if (value == null){
            return null;
        }else {
            String valueString = String.valueOf(value);
            int year = 0;
            int month = 0;
            int day = 0;
            int hour = 0;
            int minute = 0;
            int seceond = 0;
            if (NUMBER_DATE_PATTRN.matcher(valueString).matches()){
                year = Integer.valueOf(valueString.substring(0,4));
                month = Integer.valueOf(valueString.substring(4,6));
                day = Integer.valueOf(valueString.substring(6,8));
                if (valueString.length() == 14){
                    hour = Integer.valueOf(valueString.substring(8,10));
                    minute = Integer.valueOf(valueString.substring(10,12));
                    seceond = Integer.valueOf(valueString.substring(12,14));
                }
            }else if (DATE_PATTRN.matcher(valueString).matches()) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < valueString.length(); i++) {
                    char tempChar = valueString.charAt(i);
                    if (Character.isSpaceChar(tempChar) && sb.length() == 0) {
                        continue;
                    }
                    if (Character.isDigit(tempChar)) {
                        sb.append(tempChar);
                    }
                    if (!Character.isDigit(tempChar) || i == valueString.length() - 1) {
                        if (sb.length() == 4 && year == 0) {
                            year = Integer.valueOf(sb.toString());
                            sb = new StringBuffer();
                            continue;
                        }
                        if (year > 0 && month == 0) {
                            month = Integer.valueOf(sb.toString());
                            sb = new StringBuffer();
                            continue;
                        }
                        if (month > 0 && day == 0) {
                            day = Integer.valueOf(sb.toString());
                            sb = new StringBuffer();
                            continue;
                        }
                        if (day > 0 && hour == 0) {
                            hour = Integer.valueOf(sb.toString());
                            sb = new StringBuffer();
                            continue;
                        }
                        if (hour > 0 && minute == 0) {
                            minute = Integer.valueOf(sb.toString());
                            sb = new StringBuffer();
                            continue;
                        }
                        if (minute > 0 && seceond == 0) {
                            seceond = Integer.valueOf(sb.toString());
                            sb = new StringBuffer();
                        }
                    }
                }
            }else {
                throw new AutoExcelException("The date format is incorrect");
            }
            try {
                LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, seceond);
                return dateTime.format(DATE_FORMATTER);
            }catch (Exception e){
                throw new AutoExcelException("The date format is incorrect");
            }
        }
    }

你可能感兴趣的:(java,java)