运行我的程序后,我在运行大约 2 小时后发生了奇怪的崩溃,说明它无法解析日期。
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)
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?因为在网上查找时,我发现这可能是由于格式不正确造成的,但由于我没有指定格式,所以对我来说情况并非如此。
解析我的时间戳的代码如下:
Instant instant = Instant.parse(cAdvisor.getTimestamp());
Long epoch = instant.getEpochSecond();
Run Code Online (Sandbox Code Playgroud)
注意:该cAdvisor.getTimestamp()方法返回一个字符串,例如:'2016-10-26T12:31:39.084726218Z'
注 2:我的 java 版本报告了这一点
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)
Run Code Online (Sandbox Code Playgroud)
更新 #1:以下代码复制了此问题:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
生成以下堆栈跟踪
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
Run Code Online (Sandbox Code Playgroud)
对于其他遇到此问题的人,有人指出此错误的根源在于 mac 上过时的 java 版本。
为了升级此版本,我从以下位置安装了最新的 JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html。但是安装后,旧文件夹不会更新,需要手动更新。
为此,请导航至:/Library/Java/JavaVirtualMachines/您将在其中看到 2 个包含 jdk 1.8.0 的不同目录,一个名为 的目录jdk1.8.0.jdk,另一个名为 的目录jdk1.8.0_<version>.jdk,其中版本是版本号(例如 111)。
现在继续删除名为的目录jdk1.8.0.jdk(或将其移动到 _old 文件夹)并创建一个指向新目录的符号链接sudo ln -s jdk1.8.0_<version>.jdk jdk1.8.0.jdk
这为我解决了完整的问题,现在错误不再出现。非常感谢 @assylias 和 @basil-bourque 提出的建议,形成了这个解决方案。
| 归档时间: |
|
| 查看次数: |
18060 次 |
| 最近记录: |