Java Joda时间转换查询
如何使用joda time API将“2014-06-16T07:00:00.000Z”转换为“2014年6月16日07:00:00” 下面的代码正在引发异常Java Joda时间转换查询,java,jodatime,Java,Jodatime,如何使用joda time API将“2014-06-16T07:00:00.000Z”转换为“2014年6月16日07:00:00” 下面的代码正在引发异常 java.lang.IllegalArgumentException: Illegal pattern component: T at org.joda.time.format.DateTimeFormat.parsePatternTo(DateTimeFormat.java:570) at org.joda.time.f
java.lang.IllegalArgumentException: Illegal pattern component: T
at org.joda.time.format.DateTimeFormat.parsePatternTo(DateTimeFormat.java:570)
at org.joda.time.format.DateTimeFormat.createFormatterForPattern(DateTimeFormat.java:693)
at org.joda.time.format.DateTimeFormat.forPattern(DateTimeFormat.java:181)
at com.joda.JodaTimeTest.convertJodaTimezone(JodaTimeTest.java:59)
at com.joda.JodaTimeTest.main(JodaTimeTest.java:50)
代码如下:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ssZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString());
您需要将文字
T
括在单引号内。此外,毫秒的图案也不正确。您需要在毫秒内包含SSS
。有关更多信息,请查看
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
更新:要将日期时间
格式化为您选择的字符串表示形式,您需要这样做
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString()); // You get a DateTime object
// Create a new formatter with the pattern you want
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("dd-MMM-yy HH:mm:ss");
String dateStringInYourFormat = formatter2.print(dt); // format the DateTime to that pattern
System.out.println(dateStringInYourFormat); // Prints 16-Jun-14 12:30:00 because of the TimeZone I'm in
您可以自己指定时区,也可以选择默认的系统时区。您需要将文本
T
括在单引号内。此外,毫秒的图案也不正确。您需要在毫秒内包含SSS
。有关更多信息,请查看
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
更新:要将日期时间
格式化为您选择的字符串表示形式,您需要这样做
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString()); // You get a DateTime object
// Create a new formatter with the pattern you want
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("dd-MMM-yy HH:mm:ss");
String dateStringInYourFormat = formatter2.print(dt); // format the DateTime to that pattern
System.out.println(dateStringInYourFormat); // Prints 16-Jun-14 12:30:00 because of the TimeZone I'm in
您可以自己指定时区,也可以选择默认的系统时区。您需要更改
"yyyy-MM-ddTHH:mm:ssZ"
到
现在
输出:
2014-06-16T07:00:00.000+05:30
你需要改变
"yyyy-MM-ddTHH:mm:ssZ"
到
现在
输出:
2014-06-16T07:00:00.000+05:30
感谢您的更新,但获得此异常java.lang.IllegalArgumentException:无效格式:“2014-06-16T07:00:00.000Z”在“.000Z”处格式不正确。现在它工作正常,但问题很小,输出在2014-06-16T00:00:00.000-07:00末尾添加“-07:00”,我不需要它。如何解决它?谢谢你的更新,但是得到了这个异常java.lang.IllegalArgumentException:无效格式:“2014-06-16T07:00:00.000Z”在“.000Z”处格式不正确。现在它工作正常,但问题很小,输出在2014-06-16T00:00:00.000-07:00末尾添加了“-07:00”,我不需要它。如何解决它?感谢您的更新,但获得此异常java.lang.IllegalArgumentException:无效格式:“2014-06-16T07:00:00.000Z”在.000Z处格式不正确谢谢您的更新,但获得此异常java.lang.IllegalArgumentException:无效格式:“2014-06-16T07:00:00.000Z”在.000Z处格式不正确