Java Joda 时间:格式无效。数据格式错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39994170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:37:13  来源:igfitidea点击:

Joda Time: Invalid format. Data is malformed

javajodatime

提问by Deniss M.

Trying to process this string with date and time:

尝试使用日期和时间处理此字符串:

2015-10-23T00:00:00+03:00

By using this code:

通过使用此代码:

String transactionDateValue = getNodeValue(nodeList, i, "transactionDate");
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss ZZZ");
DateTime jodaTime = dateTimeFormatter.parseDateTime(transactionDateValue);
DateTimeFormatter resultFormat = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");

This is the error:

这是错误:

java.lang.IllegalArgumentException: Invalid format: "2015-10-23T00:00:00+03:00" is malformed at "T00:00:00+03:00"

    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)
    at repgen.service.PrepareExcelService.fillContent(PrepareExcelService.java:169)
    at repgen.service.PrepareExcelService.prepareDocument(PrepareExcelService.java:44)
    at repgen.service.PrepareExcelServiceTest.testPrepareExcelService(PrepareExcelServiceTest.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.mockito.internal.junit.JUnitRule.evaluate(JUnitRule.java:16)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access
DateTime jodaTime = DateTime.parse(transactionDateValue);
0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

I suspect my error is near the ZZZ parameter, but cannot solve it. I also tried the parameters ZZZZ, ZZ, but that didn't fix it.

我怀疑我的错误在 ZZZ 参数附近,但无法解决。我也试过参数 ZZZZ、ZZ,但没有解决。

采纳答案by Jesper

This happens because the string you are trying to parse contains a T, which is not in the format string.

发生这种情况是因为您尝试解析的字符串包含一个T,它不在格式字符串中。

You are trying to parse a string which is in the standard ISO 8601format. You do not need a custom date format string for this, because Joda Time already supports this format by default. Just do:

您正在尝试解析标准ISO 8601格式的字符串。您不需要为此自定义日期格式字符串,因为 Joda Time 默认已支持此格式。做就是了:

DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");

回答by Jens

Your Format must be:

您的格式必须是:

##代码##

It must be exactly like the date string, with the fixed values escaped by single quotes and without additional blanks. Also you have to use HHfor 24 hours Format. hh is 12 hours Format and it starts at 1 and Ends on 12

它必须与日期字符串完全一样,固定值由单引号转义并且没有额外的空格。您还必须使用HH24 小时格式。hh 是 12 小时格式,从 1 开始到 12 结束