LocalDateTime使用DateTimeFormatterBuilder解析短年格式dd-MMM-YY HH:mm

时间:2017-12-03 22:45:25

标签: java datetime

我一直在尝试创建DateUtil来将字符串解析为LocalDateTime。如果一年有模式" yyyy"没有问题。但如果年份代表" yy"。

,我会得到例外
Exception in thread "main" java.time.format.DateTimeParseException: Text '06-mar-17 11:52' could not be parsed at index 3
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at rinex.service.impl.utils.date.Test.parseToLocalDateTime(Test.java:43)
at rinex.service.impl.utils.date.Test.main(Test.java:53)

测试课

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeFormatterBuilder;
    import java.time.format.DateTimeParseException;
    import java.time.temporal.ChronoField;
    import java.util.LinkedHashMap;
    import java.util.Map;

    public class Test {

    private static final Map<String, String> DATE_FORMAT_REGEXPS = new LinkedHashMap<String, String>() {{
            put("^\\d{4}\\d{1,2}\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyyMMdd HH:mm:ss");
            put("^\\d{1,2}-[a-z]{3}-\\d{2}\\s\\d{1,2}:\\d{2}$", "dd-MMM-yy HH:mm");
        }};

    private static final Map<String, DateTimeFormatter> DATE_SHORT_FORMAT_REGEXPS = new LinkedHashMap<String, DateTimeFormatter>() {{
        put("dd-MMM-yy HH:mm", new DateTimeFormatterBuilder().
                appendPattern("dd-MMM-").
                appendValueReduced(ChronoField.YEAR, 2, 2, 2000).
                appendPattern(" HH:mm").toFormatter());
    }};

    public static String determineDateFormat(String dateString) {
        for (String regexp : DATE_FORMAT_REGEXPS.keySet()) {
            if (dateString.toLowerCase().matches(regexp)) {
                return DATE_FORMAT_REGEXPS.get(regexp);
            }
        }
        return null; // Unknown format.
    }

    public static LocalDateTime parseToLocalDateTime(String date) {
        date = date.trim().toLowerCase().replaceAll("utc","");
        String format = determineDateFormat(date);
        LocalDateTime local;

        try {
            local = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
        } catch (DateTimeParseException e) {
            DateTimeFormatter formatter = DATE_SHORT_FORMAT_REGEXPS.get(format);
            local = LocalDateTime.parse(date, formatter);
        }
        return local;
    }

    public static void main(String[] args) {
        String date = "20170506 11:52:00UTC";
        LocalDateTime dateTime = Test.parseToLocalDateTime(date);

        date = "06-MAR-17 11:52";
        dateTime = Test.parseToLocalDateTime(date);
    }
}

Debug for "20170506 11:52:00UTC" date - "yyyyMMdd HH:mm:ss"

Debug for "06-MAR-17 11:52" date - "dd-MMM-yy HH:mm"

那么,DateTimeFormatterBuilder解析&#34; 06-MAR-17 11:52&#34;日期?

1 个答案:

答案 0 :(得分:1)

这不是年份,而是月份的字符串表示,parsing is case sensitive by default

您可以使用正确的(标题)案例:

// works
LocalDateTime.parse("06-Mar-17 11:52",
        DateTimeFormatter.ofPattern("dd-MMM-yy HH:mm", Locale.ENGLISH));

或者您可以构建一个解析不区分大小写的DateTimeFormatter

// works
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .parseCaseInsensitive().appendPattern("dd-MMM-yy HH:mm")
        .toFormatter(Locale.ENGLISH);
formatter.parse("06-mar-17 11:52");

(如果您的默认语言环境已经是英语,则不需要显式语言环境。)