java.time.format.DateTimeParseException:无法在索引 21 处解析文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35881681/
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
java.time.format.DateTimeParseException: Text could not be parsed at index 21
提问by daydreamer
I get the datetime value as
我得到日期时间值
created_at '2012-02-22T02:06:58.147Z'
Read-only. The time at which this task was created.
Which is given by Asana API
由 Asana API 提供
I am using Java 8
to parse the date time as following
我Java 8
用来解析日期时间如下
import java.time.*;
import java.time.format.*;
public class Times {
public static void main(String[] args) {
final String dateTime = "2012-02-22T02:06:58.147Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SX");
final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter);
System.out.println(parsed);
}
}
When I run this, I get the following error
当我运行它时,我收到以下错误
Exception in thread "main" java.time.format.DateTimeParseException: Text '2012-02-22T02:06:58.147Z' could not be parsed at index 21
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at Times.main(Times.java:11)
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:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
What is not right here?
这里有什么不对?
采纳答案by assylias
The default parser can parse your input. So you don't need a custom formatter and
默认解析器可以解析您的输入。所以你不需要自定义格式化程序和
String dateTime = "2012-02-22T02:06:58.147Z";
ZonedDateTime d = ZonedDateTime.parse(dateTime);
works as expected.
按预期工作。
回答by daydreamer
The following worked for me
以下对我有用
import java.time.*;
import java.time.format.*;
public class Times {
public static void main(String[] args) {
final String dateTime = "2012-02-22T02:06:58.147Z";
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter.withZone(ZoneId.of("UTC")));
System.out.println(parsed.toLocalDateTime());
}
}
and gave me output as
并给了我输出
2012-02-22T02:06:58.147
回答by Meno Hochschild
Your original problem was wrong pattern symbol "h" which stands for the clock hour (range 1-12). In this case, the am-pm-information is missing. Better, use the pattern symbol "H"instead (hour of day in range 0-23). So the pattern should rather have been like:
您最初的问题是错误的模式符号“h”,它代表时钟小时(范围 1-12)。在这种情况下,缺少 am-pm-information。更好的是,使用模式符号“H”代替(一天中的小时在 0-23 范围内)。所以模式应该是这样的:
uuuu-MM-dd'T'HH:mm:ss.SSSX (best pattern also suitable for strict mode)
uuuu-MM-dd'T'HH:mm:ss.SSSX(最好的模式也适用于严格模式)
回答by Roman
If your input always has a time zone of "zulu" ("Z" = UTC), then you can use DateTimeFormatter.ISO_INSTANT
(implicitly):
如果您的输入始终具有“zulu”(“Z”= UTC)的时区,那么您可以使用DateTimeFormatter.ISO_INSTANT
(隐式):
final Instant parsed = Instant.parse(dateTime);
If time zone varies and has the form of "+01:00" or "+01:00:00" (when not "Z"), then you can use DateTimeFormatter.ISO_OFFSET_DATE_TIME
:
如果时区变化并且具有“+01:00”或“+01:00:00”的形式(当不是“Z”时),那么您可以使用DateTimeFormatter.ISO_OFFSET_DATE_TIME
:
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
final ZonedDateTime parsed = ZonedDateTime.parse(dateTime, formatter);
If neither is the case, you can construct a DateTimeFormatter
in the same manner as DateTimeFormatter.ISO_OFFSET_DATE_TIME
is constructed.
如果两者都不是,您可以DateTimeFormatter
按照与DateTimeFormatter.ISO_OFFSET_DATE_TIME
构造相同的方式构造 a 。
Your current pattern has several problems:
您当前的模式有几个问题:
- not using strict mode (
ResolverStyle.STRICT
); - using
yyyy
instead ofuuuu
(yyyy
will not work in strict mode); - using 12-hour
hh
instead of 24-hourHH
; - using only one digit
S
for fractional seconds, but input has three.
- 不使用严格模式(
ResolverStyle.STRICT
); - 使用
yyyy
而不是uuuu
(yyyy
在严格模式下不起作用); - 使用 12 小时
hh
而不是 24 小时HH
; S
小数秒只使用一位,但输入有三位。