【问题标题】:Instant Time parsing error (Unable to obtain Instant from TemporalAccessor)Instant Time 解析错误(无法从 TemporalAccessor 获取 Instant)
【发布时间】:2017-03-09 19:27:24
【问题描述】:

运行我的程序后,我在运行它大约 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

【问题讨论】:

  • 什么是 cAdvisor?
  • 嗨@Sikorski我在问题中添加了这个解释:)
  • 1477485099 是我将此字符串粘贴到完全相同的代码中并在我的机器上运行时的结果。
  • 这段代码也适用于我,但在运行大约 500 次后它开始崩溃。似乎有一些缓冲区没有被重置或导致它的原因。
  • @XavierGeerinck 我无法重现您的问题,但我怀疑问题出在:java version "1.8.0-ea"!这是一个早期访问版本(即测试版),可能已经有 2 年了,如果不是更多的话。我建议升级到最新版本的 Java 8。

标签: java java-time


【解决方案1】:

对于遇到此问题的其他人,有人指出此错误的根源在于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_&lt;version&gt;.jdk 的目录,其中版本是版本号(例如 111)。

现在继续删除名为 jdk1.8.0.jdk 的目录(或将其移至 _old 文件夹)并创建一个符号链接,指向带有 sudo ln -s jdk1.8.0_&lt;version&gt;.jdk jdk1.8.0.jdk 的新目录

这为我解决了完整的问题,现在错误不再出现。非常感谢 @assylias 和 @basil-bourque 提出的导致此解决方案的建议。

【讨论】:

    【解决方案2】:

    您的本地JVMIDEOS 似乎存在本地问题。我建议您删除并重新安装您的 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 上运行

    查看您的代码稍作修改的版本成功运行live on 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
    

    【讨论】:

    • 好的,我会接受这个答案,因为没有其他解决方案对我有用,这是一个奇怪的错误。感谢您的投入和时间的帮助! :)
    • 我添加了自己的答案,为我修复了它,因为将其更新到最新版本没有帮助。感谢@basil-bourque 的建议!它导致了我的解决方案:)
    猜你喜欢
    • 2020-12-24
    • 2019-12-07
    • 2019-12-04
    • 2019-07-22
    • 2014-11-07
    • 2016-06-26
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多