7

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

Does anyone knows why it gives this? Since when looking online, I found that it could be due to an incorrect format, but since I did not specify the format this is not the case for me.

The code that parses my timestamp is the following:

Instant instant = Instant.parse(cAdvisor.getTimestamp());
Long epoch = instant.getEpochSecond();

Note: The cAdvisor.getTimestamp() method returns a string such as: '2016-10-26T12:31:39.084726218Z'

Note 2: My java version reports this

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)

Update #1: The following code replicates this issue:

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);
        }
    }
}

Which generates following stacktrace

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
assylias
  • 321,522
  • 82
  • 660
  • 783
Xavier Geerinck
  • 606
  • 1
  • 6
  • 18
  • what is cAdvisor ? – Sikorski Oct 27 '16 at 07:55
  • Hi @Sikorski I added the explanation of this to the question :) – Xavier Geerinck Oct 27 '16 at 07:57
  • 1477485099 is the result when I pasted this string in the exact same code and ran it on my machine. – Axl Oct 27 '16 at 08:05
  • This code is also working for me, but after running it around 500 times it starts crashing. It seems that there is some buffer that doesn't get reset or something that causes it. – Xavier Geerinck Oct 27 '16 at 08:12
  • I put it an loop for over a million times even with varying nanoseconds and no crash. Edit: I also get no crash from your Update#1 it doesn't replicate the issue on my machine. – Axl Oct 27 '16 at 08:49
  • 4
    @XavierGeerinck I can't reproduce your problem but I suspect the problem is this: *java version "1.8.0-ea"*! That's an early access build (i.e. a beta version) that's probably 2 years old, if not more. I suggest upgrading to the latest version of Java 8. – assylias Oct 27 '16 at 08:53
  • @assylias indeed, after updating my JDK (Manually on mac) it doesn't occur anymore. Thanks! – Xavier Geerinck Oct 27 '16 at 09:09
  • @assylias can you please post your comment as an answer. – Sikorski Oct 27 '16 at 11:00
  • Ok I removed my answer, it is still not working :/ – Xavier Geerinck Oct 27 '16 at 17:39
  • @XavierGeerinck Your code runs successfully for me, Java 8 Update 111 in NetBeans 8.2 on macOS El Capitan. I increased the iterations to 1_000_000 and still no problem. I noticed your stack trace says IntelliJ. Perhaps your IDE is building and/or running against the wrong JDK. I suggest you try running on a VM or other computer, and if no problems, clean out your primary machine deleting all JDKs and IDEs and re-installing. – Basil Bourque Oct 30 '16 at 00:26

2 Answers2

4

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.

Xavier Geerinck
  • 606
  • 1
  • 6
  • 18
2

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Ok, I will accept this answer, seeing that no other solution worked for me and it is a strange error. Thanks for the input and the time for helping! :) – Xavier Geerinck Oct 30 '16 at 19:22
  • I added my own answer which fixed it for me, since updating it to the latest version didn't help. Thanks @basil-bourque for the suggestion! it lead to my solution :) – Xavier Geerinck Nov 15 '16 at 10:06