需要使用joda灵活的datetime转换 [英] need flexible datetime conversion with joda

查看:180
本文介绍了需要使用joda灵活的datetime转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用joda解析电子邮件中的datetime字符串。不幸的是,我得到各种不同的格式,例如

I want to use joda to parse datetime strings in emails. Unfortunately I get all kinds of different formats, for example

Wed, 19 Jan 2011 12:52:31 -0600
Wed, 19 Jan 2011 10:15:34 -0800 (PST)
Wed, 19 Jan 2011 20:03:48 +0000 (UTC)
Wed, 19 Jan 2011 17:02:08 -0600 (CST)
Fri, 21 Jan 2011 10:39:55 +0100 (CET)
Fri, 21 Jan 2011 17:50:42 -0500 (EST)
Wed, 06 Apr 2011 15:38:25 GMT
Thu, 7 Apr 2011 11:38:24 +0200 
Fri,  8 Apr 2011 05:13:36 -0700 (MST)
20 Apr 2011 03:00:46 -0400

下面的代码捕获大多数变体,但不是全部(例如,当有两个空格而不是一个空格时,逗号缺少等等)。而且看起来很尴尬。

The code below catches most of the variants but not all (for example, when there are two spaces instead of one, when the comma is missing etc.). And it looks just awkward.

有更优雅的方式来处理吗?请指教。

Is there a more elegant way to handle this? Please advise.

            DateTimeParser[] parsers = {
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(CET)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(CST)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(CEST)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(GMT)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(MST)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(PST)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(UTC)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(EST)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(EDT)'").getParser(),
                    DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '(CDT)'").getParser(),
            };
            DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();

            try {
                calendar = inputFormatter.withLocale(Locale.US).parseDateTime(date[0]);
            }
            catch(Exception e) {
                System.out.println("problem with " + date[0]);
            }


推荐答案

使用Joda的 DateTimeParser 你自己和本质上解析文本自己建立一个有效的DateTime(我认为这将是很多工作),我不认为你的方法真的很错了。我认为你有太多的格式。我想你的格式可以减少到:

Outside of using Joda's DateTimeParser yourself and essentially parsing the text yourself building up a valid DateTime (which i think would be a lot of work), i don't think there's really much wrong with your approach. I do think that you have too many formats though. I think your set of formats could be reduced to:

 DateTimeParser[] parsers = {
     DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z").getParser(),
     DateTimeFormat.forPattern("E, d MMM y HH:mm:ss Z '('z')'").getParser(),
     DateTimeFormat.forPattern("E, d MMM y HH:mm:ss z").getParser(),
     DateTimeFormat.forPattern("dd MMM y HH:mm:ss Z").getParser(),
 };

Z(Capital-Z)是RFC 822数字时区,small-z是时区,例如PDT。这仍然是(平均)每个解析请求抛出2个异常,但是如果不需要高性能,那可能不是那么糟糕。

Z (Capital-Z) is the RFC 822 numeric timezone and small-z is the acronym for the timezone, like PDT, for example. This is still (on average) 2 exceptions thrown per parse request but if this doesn't need to be high-performance, that's probably not so bad.

这篇关于需要使用joda灵活的datetime转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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