Joda String DateTime to DateTime

时间:2014-08-14 07:38:26

标签: java datetime jodatime

我试图转换像下面那样传递的DateTime字符串

"2013-08-14T12:54:57.908+05:30"

我的转换器功能看起来像这样

public static DateTime stringDateToJodaDateTime(String strDate){
        DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE, dd MMM YYYY HH:mm:ss Z");
        DateTime jodaDateTime = formatter.parseDateTime(strDate);
        return jodaDateTime;
    }

它给我以下错误

Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:113)
    ... 88 more
Caused by: java.lang.IllegalArgumentException: Invalid format: "2013-08-14T12:54:57.908+05:30"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899)
    at com.kpowd.utility.UserUtility.stringDateToJodaDateTime(UserUtility.java:65)
    at com.kpowd.controller.CommonChartController.init(CommonChartController.java:51)
    ... 93 more

我认为因为错误的“forPattern”, 我无法更改传递的字符串DateTime Pattern。我所能做的就是改变“DateTimeFormat.forPattern”,帮帮我

1 个答案:

答案 0 :(得分:6)

<强>问题:

DateTimeFormat.forPattern("EEE, dd MMM YYYY HH:mm:ss Z")

这基本上是一个错误的模式,用于解析你的String date,从而在解析它时捕获Invalid format

<强>溶液

yyyy-MM-dd'T'HH:mm:ss.SSSZ