Instant Time parsing error (Unable to obtain Instant from TemporalAccessor)

前端 未结 2 462
忘了有多久
忘了有多久 2021-01-21 07:45

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.0         


        
相关标签:
2条回答
  • 2021-01-21 07:53

    You appear to have a local problem with your local JVM, IDE, or OS. I suggest you delete and reinstall your IDE and all your JVMs.

    Runs on my computer

    I run the following code with ten times the number of your loops (a million) with no problems, no errors. I am using Java 8 Update 111 in NetBeans 8.2 on macOS El Capitan on a MacBook Pro Retina (late 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 );
            }
        }
    }
    

    Runs on IdeOne.com

    See a slightly modified version of your code running successfully 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() );
        }
    }
    

    When run.

    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
    
    0 讨论(0)
  • 2021-01-21 08:07

    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.

    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).

    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

    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.

    0 讨论(0)
提交回复
热议问题