在 Java 中解析任何日期 [英] Parse any date in Java

查看:45
本文介绍了在 Java 中解析任何日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题问了很多,显然你不能解析任何任意的日期.但是,我发现 python-dateutil 库能够解析我抛出的每个日期,同时在找出日期格式字符串方面完全不需要付出任何努力.Joda 时间总是被认为是一个很棒的 Java 日期解析器,但它仍然需要您在选择格式(或创建自己的格式)之前决定日期的格式.您不能只调用 DateFormatter.parse(mydate) 并神奇地取回 Date 对象.

I know this question is asked quite a bit, and obviously you can't parse any arbitrary date. However, I find that the python-dateutil library is able to parse every date I throw at it, all while requiring absolutely zero effort in figuring out a date format string. Joda time is always sold as being a great Java date parser, but it still requires you to decide what format your date is in before you pick a Format (or create your own). You can't just call DateFormatter.parse(mydate) and magically get a Date object back.

例如,日期Wed Mar 04 05:09:06 GMT-06:00 2009"可以用 python-dateutil 正确解析:

For example, the date "Wed Mar 04 05:09:06 GMT-06:00 2009" is properly parsed with python-dateutil:

import dateutil.parser
print dateutil.parser.parse('Wed Mar 04 05:09:06 GMT-06:00 2009')

但以下 Joda 时间调用不起作用:

but the following Joda time call doesn't work:

    String date = "Wed Mar 04 05:09:06 GMT-06:00 2009";
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    DateTime dt = fmt.parseDateTime(date);
    System.out.println(date);

创建您自己的 DateTimeFormatter 违背了目的,因为这似乎与使用带有正确格式字符串的 SimpleDateFormatter 相同.

And creating your own DateTimeFormatter defeats the purpose, since that seems to be the same as using SimpleDateFormatter with the correct format string.

是否有类似的方法来解析 Java 中的日期,例如 python-dateutil?我不在乎错误,我只希望它最完美.

Is there a comparable way to parse a date in Java, like python-dateutil? I don't care about errors, I just want it to mostly perfect.

推荐答案

你最好的办法是寻求帮助,让正则表达式匹配日期格式模式和/或进行暴力破解.

Your best bet is really asking help to regex to match the date format pattern and/or to do brute forcing.

几年前我写了一个有点傻的DateUtil 完成了这项工作.以下是相关性的摘录:

Several years ago I wrote a little silly DateUtil class which did the job. Here's an extract of relevance:

private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {{
    put("^\d{8}$", "yyyyMMdd");
    put("^\d{1,2}-\d{1,2}-\d{4}$", "dd-MM-yyyy");
    put("^\d{4}-\d{1,2}-\d{1,2}$", "yyyy-MM-dd");
    put("^\d{1,2}/\d{1,2}/\d{4}$", "MM/dd/yyyy");
    put("^\d{4}/\d{1,2}/\d{1,2}$", "yyyy/MM/dd");
    put("^\d{1,2}\s[a-z]{3}\s\d{4}$", "dd MMM yyyy");
    put("^\d{1,2}\s[a-z]{4,}\s\d{4}$", "dd MMMM yyyy");
    put("^\d{12}$", "yyyyMMddHHmm");
    put("^\d{8}\s\d{4}$", "yyyyMMdd HHmm");
    put("^\d{1,2}-\d{1,2}-\d{4}\s\d{1,2}:\d{2}$", "dd-MM-yyyy HH:mm");
    put("^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{2}$", "yyyy-MM-dd HH:mm");
    put("^\d{1,2}/\d{1,2}/\d{4}\s\d{1,2}:\d{2}$", "MM/dd/yyyy HH:mm");
    put("^\d{4}/\d{1,2}/\d{1,2}\s\d{1,2}:\d{2}$", "yyyy/MM/dd HH:mm");
    put("^\d{1,2}\s[a-z]{3}\s\d{4}\s\d{1,2}:\d{2}$", "dd MMM yyyy HH:mm");
    put("^\d{1,2}\s[a-z]{4,}\s\d{4}\s\d{1,2}:\d{2}$", "dd MMMM yyyy HH:mm");
    put("^\d{14}$", "yyyyMMddHHmmss");
    put("^\d{8}\s\d{6}$", "yyyyMMdd HHmmss");
    put("^\d{1,2}-\d{1,2}-\d{4}\s\d{1,2}:\d{2}:\d{2}$", "dd-MM-yyyy HH:mm:ss");
    put("^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{2}:\d{2}$", "yyyy-MM-dd HH:mm:ss");
    put("^\d{1,2}/\d{1,2}/\d{4}\s\d{1,2}:\d{2}:\d{2}$", "MM/dd/yyyy HH:mm:ss");
    put("^\d{4}/\d{1,2}/\d{1,2}\s\d{1,2}:\d{2}:\d{2}$", "yyyy/MM/dd HH:mm:ss");
    put("^\d{1,2}\s[a-z]{3}\s\d{4}\s\d{1,2}:\d{2}:\d{2}$", "dd MMM yyyy HH:mm:ss");
    put("^\d{1,2}\s[a-z]{4,}\s\d{4}\s\d{1,2}:\d{2}:\d{2}$", "dd MMMM yyyy HH:mm:ss");
}};

/**
 * Determine SimpleDateFormat pattern matching with the given date string. Returns null if
 * format is unknown. You can simply extend DateUtil with more formats if needed.
 * @param dateString The date string to determine the SimpleDateFormat pattern for.
 * @return The matching SimpleDateFormat pattern, or null if format is unknown.
 * @see SimpleDateFormat
 */
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.
}

(咳咳,双括号初始化,咳咳,这只是为了让它全部适应 100 个字符的最大长度;))

(cough, double brace initialization, cough, it was just to get it all to fit in 100 char max length ;) )

您可以使用新的正则表达式和日期格式模式轻松地自行扩展它.

You can easily expand it yourself with new regex and dateformat patterns.

这篇关于在 Java 中解析任何日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆