javadatetimelong-integernanotimesystem-clock

How to calculate nanoseconds from 1800


I'm trying to calculate the number of nano seconds that passed from: 11-Nov-1831 00:00:00.00

to a given date.

I tried to use alot of packages but none of them succed in the mission (the closest was Instant)

What's the best way to achive this mission?


Solution

  • Instant is still the best option, but you may need some helper objects.

    For instance, to create the start date (replace the Z with a different time zone or offset as needed):

    Instant start = Instant.parse("1831-11-11T00:00:00Z");
    

    Then take the given date. You can parse it, or use conversion methods. For instance, if you have a LocalDateTime (use a different ZoneId or ZoneOffset as needed):

    LocalDateTime localDateTime = LocalDateTime.parse("2022-06-09T20:56");
    Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    

    Now get a duration:

    Duration duration = Duration.between(start, instant);
    long nanos = duration.toNanos();