2

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
user3927415
  • 415
  • 4
  • 18
  • 5
    Most systems can't represent a time less than a nano second. I doubt that Java's date time system supports anything smaller than a nano second. – vandench Aug 29 '18 at 17:10
  • 1
    @vandench That's because a light nanosecond (distance traveled by light in a nanosecond) is about half the width of the average laptop. There's very little chance of a smaller unit of time ever being relevant to a computer or its users. – mypetlion Aug 29 '18 at 17:15
  • @vandench How can i assert that java supports that? – user3927415 Aug 29 '18 at 17:19
  • @mypetlion ok... Not sure how any of this relates to light, but ok. Also that is definitely not the reason why computers don't support times less than a nanosecond, they don't support times that small because their processors can't perform at speeds where that would be applicable. – vandench Aug 29 '18 at 17:21
  • @user3927415 How can you assert that? You can look at the source code. – vandench Aug 29 '18 at 17:21
  • 3
    @vandench I was just explaining the reasoning behind the thing you said. It's impossible to get a signal from one piece of hardware to another, or even from one part of a piece of hardware to another, in significantly less time than a nanosecond. CPU speed is ultimately limited by the speed of light, as is everything in our universe. Light will travel 0.3mm in a picosecond in a vacuum. A CPU is not a vacuum, and is larger than 0.3mm. Meaning that measurements of time that small will likely never be relevant. – mypetlion Aug 29 '18 at 17:50

1 Answers1

4

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

  • Parse the first part as a LocalDateTime or Instant or a count of whole seconds since an epoch such as 1970-01-01T00:00:00Z.
  • Then parse the second part as 64-bit long integer.

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154