开发者

joda DateTime parser error

I use jodatime to parse date time strings as follows:

    public static void 开发者_开发知识库main(String[]args){
        String s ="16-Jul-2009 05:20:18 PDT";
        String patterns = "dd-MMM-yyyy HH:mm:ss z";

            DateTimeFormatter fm = DateTimeFormat.forPattern(patterns);
            DateTime d=fm.parseDateTime(s);
            System.out.println(d);

    }

I get

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "16-Jul-2009 05:20:18 PDT" is malformed at "PDT"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)

what's wrong? how to parse the timezone properly?


From the DateTimeFormat javadoc:

The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported. All ASCII letters are reserved as pattern letters, which are defined as follows:

Your best bet is to fall back to SimpleDateFormat and then construct DateTime based on Date#getTime().

String s = "16-Jul-2009 05:20:18 PDT";
String pattern = "dd-MMM-yyyy HH:mm:ss z";
Date date = new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s);
DateTime d = new DateTime(date.getTime());
System.out.println(d);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜