即时时间解析错误(无法从TemporalAccessor获取即时信息) [英] Instant Time parsing error (Unable to obtain Instant from TemporalAccessor)
问题描述
运行程序后,运行大约2个小时后,我收到了奇怪的崩溃消息,提示它无法解析日期.
After running my program, I get this weird crash occurring after around 2 hours of running it stating that it can't parse the date.
Text '2016-10-26T12:31:39.084726218Z' could not be parsed: Unable to obtain Instant from TemporalAccessor: {InstantSeconds=1477485099, NanoOfSecond=84726218},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919)
有人知道为什么会这样吗?自从在网上查找时,我发现可能是由于格式不正确,但是由于我没有指定格式,所以我不是这种情况.
Does anyone knows why it gives this? Since when looking online, I found that it could be due to an incorrect format, but since I did not specify the format this is not the case for me.
解析我的时间戳的代码如下:
The code that parses my timestamp is the following:
Instant instant = Instant.parse(cAdvisor.getTimestamp());
Long epoch = instant.getEpochSecond();
注意:cAdvisor.getTimestamp()
方法返回一个字符串,例如:'2016-10-26T12:31:39.084726218Z'
Note: The cAdvisor.getTimestamp()
method returns a string such as: '2016-10-26T12:31:39.084726218Z'
注2:我的Java版本对此进行了报告
Note 2: My java version reports this
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b115)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b57, mixed mode)
更新1:以下代码复制了此问题:
Update #1: The following code replicates this issue:
import java.time.Instant;
import java.util.Random;
// one class needs to have a main() method
public class Test
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
Random rand = new Random();
for (int i = 0; i < 100000; i++) {
String date = "2016-10-26T12:31:39.0847";
for (int j = 0; j < rand.nextInt(6); j++) {
date += rand.nextInt(10);
}
date += "Z";
date = date.replace("26", "" + (rand.nextInt(20) + 10));
Instant instant = Instant.parse(date);
Long epoch = instant.getEpochSecond();
System.out.println(epoch);
}
}
}
哪个会生成以下堆栈跟踪
Which generates following stacktrace
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-10-27T12:31:39.0847Z' could not be parsed: Unable to obtain Instant from TemporalAccessor: {InstantSeconds=1477571499, NanoOfSecond=84700000},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1854)
at java.time.Instant.parse(Instant.java:392)
at Test.main(Test.java:23)
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:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {InstantSeconds=1477571499, NanoOfSecond=84700000},ISO of type java.time.format.Parsed
at java.time.Instant.from(Instant.java:375)
at java.time.Instant$$Lambda$7/1018081122.queryFrom(Unknown Source)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
... 7 more
推荐答案
对于其他有此问题的人,指出此错误的根源在于Mac上过时的Java版本.
For others having this problem, it was pointed out that the origin of this bug lies in the outdated java version on mac.
To upgrade this version, I installed the latest JDK from: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. However after installing this, the old folder doesn't get updated and requires a manual update.
为此,请导航至:/Library/Java/JavaVirtualMachines/
,您将在其中看到2个包含jdk 1.8.0的不同目录,一个名为jdk1.8.0.jdk
的目录和一个名为jdk1.8.0_<version>.jdk
的目录,其中version是发行版号(例如111).
To do this, navigate to: /Library/Java/JavaVirtualMachines/
where you will see 2 different directories containing jdk 1.8.0, a directory named jdk1.8.0.jdk
and one named jdk1.8.0_<version>.jdk
where version is the release number (for example 111).
现在继续删除名为jdk1.8.0.jdk
的目录(或将其移至_old文件夹),并使用sudo ln -s jdk1.8.0_<version>.jdk jdk1.8.0.jdk
Now go ahead and remove the directory called jdk1.8.0.jdk
(or move it to a _old folder) and create a symlink pointing towards the new one with sudo ln -s jdk1.8.0_<version>.jdk jdk1.8.0.jdk
这为我解决了完整的问题,现在错误不再出现.非常感谢@assylias和@ basil-bourque提出了导致此解决方案的建议.
This solved the complete problem for me and now the error is not appearing anymore. A big thanks to @assylias and @basil-bourque for the suggestion that lead to this solution.
这篇关于即时时间解析错误(无法从TemporalAccessor获取即时信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!