运行我的程序后,我发现这个奇怪的崩溃发生在大约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)
有谁知道它为什么会这样?从网上看,我发现它可能是由于格式不正确,但由于我没有指定格式,因此对我来说并非如此.
解析我的时间戳的代码如下:
Instant instant = Instant.parse(cAdvisor.getTimestamp());
Long epoch = instant.getEpochSecond();
注意: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)
更新#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);
}
}
}
这会产生以下堆栈跟踪
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
解决方法:
对于有这个问题的其他人,有人指出这个bug的起源在于mac上过时的java版本.
要升级此版本,我安装了最新的JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.但是在安装此文件后,旧文件夹不会更新,需要手动更新.
为此,请导航到:/ Library / Java / JavaVirtualMachines /,您将看到包含jdk 1.8.0的2个不同目录,名为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提出的导致此解决方案的建议.
标签:java,java-time 来源: https://codeday.me/bug/20190823/1702638.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。