![](/img/trans.png)
[英]Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2018-01-01 of type java.time.format.Parsed
[英]Instant Time parsing error (Unable to obtain Instant from TemporalAccessor)
運行我的程序后,我發現這個奇怪的崩潰發生在大約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的兩個不同目錄,一個名為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提出的導致此解決方案的建議。
您似乎在本地JVM , IDE或操作系統上存在本地問題。 我建議您刪除並重新安裝IDE和所有 JVM。
我運行以下代碼,循環次數為十次(一百萬次),沒有任何問題,沒有錯誤。 我在MacBook Pro Retina上的macOS El Capitan上使用NetBeans 8.2中的Java 8 Update 111(2013年末)。
import java.time.Instant;
import java.util.Random;
public class InstantCrash {
// 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 < 1_000_000 ; 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 );
}
}
}
IdeOne.com
運行 查看在IdeOne.com上成功運行的代碼的略微修改版本。
import java.util.*;
import java.lang.*;
import java.io.*;
import java.time.Instant;
import java.util.Random;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println( "Starting at: " + Instant.now() );
Random rand = new Random();
for (int i = 0; i < 1_000_000; i++) {
String date = "2016-10-26T12:31:39.0847";
for (int j = 0; j < rand.nextInt(6); j++) {
date += rand.nextInt(10);
}
if( ( i % 100_000 ) == 0 ) {
System.out.println( "So far: " + i + " | date: " + date ) ;
}
date += "Z";
date = date.replace("26", "" + (rand.nextInt(20) + 10));
Instant instant = Instant.parse(date);
Long epoch = instant.getEpochSecond();
// System.out.println(epoch); // Exceeds limit of IdeOne.com.
}
System.out.println( "Done. Now: " + Instant.now() );
}
}
跑步時
Starting at: 2016-10-30T00:46:34.439Z
So far: 0 | date: 2016-10-26T12:31:39.08476
So far: 100000 | date: 2016-10-26T12:31:39.08478
So far: 200000 | date: 2016-10-26T12:31:39.08475
So far: 300000 | date: 2016-10-26T12:31:39.0847827
So far: 400000 | date: 2016-10-26T12:31:39.0847
So far: 500000 | date: 2016-10-26T12:31:39.0847
So far: 600000 | date: 2016-10-26T12:31:39.0847
So far: 700000 | date: 2016-10-26T12:31:39.084709
So far: 800000 | date: 2016-10-26T12:31:39.0847865
So far: 900000 | date: 2016-10-26T12:31:39.084748
Done. Now: 2016-10-30T00:46:37.698Z
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.