javadate-parsingtime-formatlocaldatedatetimeformatinfo

java parse date with picosecond


I have a string date with picoseconds (between 9 to 12 digits after the period), and when I try to parse the date with the DateTimeFormatter :

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSSSSS");

it throws an exception which says that i cant create a format with more than 9 digits after the period.

Is there a another class which can parse the date, or a different way to parse.

Any solution will help me, but prefer classes / ways which work with LocalDateTime


Solution

  • No solution

    I have heard of no class, library, or database that represents picosecond resolution of time.

    As commented on the Question, and as famously taught by Dr. Grace Hopper, light travels only about a foot in a nanosecond.

    Light travels but a mere fraction of a millimeter in a picosecond. Think of the width of an individual grain of ground pepper, as Hopper showed in lectures. This is smaller than the distance taken by computer technology to move electrical signals around. So such a fine measurement of time is not likely to be practical or relevant in our lifetime.

    Truncate

    If the picos are not really necessary to your application, I suggest truncating your string, lopping off any digits after the first nine of the fractional second. Then parse as a Instant or LocalDateTime etc.

    Roll-your-own class

    If you must represent the picoseconds, I suggest splitting your input string in two, based on the decimal fraction delimiter (which under ISO 8601 standard can be either a comma or a period FULL STOP, with comma preferred).

    You could write a little class named something like PicoMoment, with a parse method, to represent these two parts internally. Look to the OpenJDK source code to see similar representations made internally in the java.time classes.

    If you go this route, I suggest you follow the java.time API as a model.