The Answer by Cardinal System is correct.
Duration
An alternative is use of Duration
class with LocalDateTime#plus
.
A Duration
object represents a span of time not attached to the timeline, on a scale of hours, minutes, seconds, and fractional second.
LocalDateTime ldt = LocalDateTime.parse( "2022-01-01 00:00:00.123".replace( " " , "T" ) ) ; // Replace SPACE with `T` to comply with standard ISO 8601 format.
Duration duration = Duration.ofMillis( 1L ) ;
LocalDateTime later = ldt.plus( duration ) ;
By the way, be aware that LocalDateTime
is inherently ambiguous. That class represents a date with a time-of-day but lacks a time zone or offset-from-UTC needed to determine a point on the timeline.
For a specific point on the timeline, use Instant
.
Instant instant = Instant.now() ;
Instant later = instant.plus( duration ) ;
To see that same moment through the lens of a particular time zone, apply a ZoneId
to get a ZonedDateTime
object.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = later.atZone( z ) ;
See this code run at Ideone.com. Note the differences: (a) one millisecond later, and (b) different time-of-day & different date.
instant.toString(): 2023-03-29T19:12:16.727887Z
later.toString(): 2023-03-29T19:12:16.728887Z
zdt.toString(): 2023-03-30T08:12:16.728887+13:00[Pacific/Auckland]
If you want only milliseconds, you can truncate to clear the microseconds & nanoseconds to zero.
Instant trunc = instant.truncatedTo( ChronoUnit.MILLIS ) ;
ldt.plus(1, ChronoUnit.MILLIS)
. – DidynamousLocalDateTime
. – Electronics