tl;dr
After correcting multiple typos in the example data of your Question, we find no errors, no surprises, when running the code. Your millisecond appears as expected.
Instant.ofEpochMilli( 1_636_334_664_001L ).toString()
2021-11-08T01:24:24.001Z
LocalDateTime
not helpful in your case
LocalDateTime
is the wrong class to use here. That class represents a date with time-of-day but lacks the context of a time zone or offset-from-UTC. So that class cannot represent a moment, a specific point on the timeline.
To track moments use:
Instant
OffsetDateTime
ZonedDateTime
Use Instant
Assuming your long values represent a count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z, use Instant.ofEpochMilli
.
Your 1636334664000l
example presumably has a typo, one too many zero digits. I will go with 163633466400l
.
When hard-coding such long
values, use the optional underscore (_
) digit grouping feature in Java. And append an L
to ensure parsing as a long
primitive.
Instant created = Instant.ofEpochMilli( 1_636_334_664_000L ) ;
Instant modified = Instant.ofEpochMilli( 1_636_334_664_001L ) ;
Calculate elapsed time.
Duration duration = Duration.between( created , modified ) ;
We expect to see a single millisecond as our result. The result is presented in standard ISO 8601 format.
Dump to console.
System.out.println( created ) ;
System.out.println( modified ) ;
System.out.println( duration ) ;
Execute at Ideone.com. We see your expected fractional second, a millisecond.
2021-11-08T01:24:24Z
2021-11-08T01:24:24.001Z
PT0.001S
ZonedDateTime
See that same moment through the wall-clock time of a particular time zone.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdtModifiedTokyo = instant.atZone( z ) ;
We continue to see your fractional second, a single millisecond.
zdtModifiedTokyo.toString(): 2021-11-08T10:24:24.001+09:00[Asia/Tokyo]