本文介绍了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?

推荐答案

来自:

From the DateTimeFormat javadoc:

您最好的选择是回退到 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解析器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:17