Search code examples
javatimedatetime-conversion

How to get nanoseconds since epoch for string formatted time "2019-01-25 14:34:34.123456789" in java?


I am able to parse 2019-01-25 14:34:34.123456789 string and get the object of ZonedDateTime. Now, I want to get time in nanoseconds precision level since epoch from this ZonedDateTime object.


Solution

  • Duration might help:

    Something like:

    Duration.between(
      ZonedDateTime.ofInstant(Instant.EPOCH, ZoneId.of("UTC")),
      yourZonedDatetime
    ).toNanos()