joda DateTime解析器错误 [英] joda DateTime parser error

查看:109
本文介绍了joda DateTime解析器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jodatime来解析日期时间字符串,如下所示:

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);

    }

我得到


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?

推荐答案

来自 DateTimeFormat javadoc

From the DateTimeFormat javadoc:


模式语法主要与 java.text.SimpleDateFormat 兼容 - 时区名称无法解析,支持更多符号。所有ASCII字母都保留为模式字母,其定义如下:

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:

您最好的选择是回退到 SimpleDateFormat 然后根据 Date#getTime()构建 DateTime

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);

这篇关于joda DateTime解析器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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