LocalDateTime add millisecond
Asked Answered
A

2

6

I want to increase the millisecond value with LocalDateTime. I used plusNanos because I didn't have plusmillisecond. I wonder if this is the right way. I'm using JDK 1.8. I also want to know if there is a plus millisecond function in later versions.

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime ldt = LocalDateTime.parse("2022-01-01 00:00:00.123",f);
        
System.out.println(ldt.format(f));
        
ldt = ldt.plusNanos(1000000);
        
System.out.println(ldt.format(f));
2022-01-01 00:00:00.123
2022-01-01 00:00:00.124
Alphabetical answered 28/3, 2023 at 12:50 Comment(2)
You can ldt.plus(1, ChronoUnit.MILLIS).Didynamous
If there is such a function in later versions: No. You can look that up in the API documentation. For example Java SE 20 API docs for LocalDateTime.Electronics
M
14

Adding nanoseconds is a completely valid way of doing this. If you want a solution that is more self-explanatory, you can use LocalDateTime#plus(long, TemporalUnit):

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.parse("2022-01-01 00:00:00.123", FORMATTER);
    localDateTime = localDateTime.plus(1L, ChronoUnit.MILLIS);
    //         How much you are adding ^              ^ What you are adding

    System.out.println(FORMATTER.format(localDateTime));
}

The TemporalUnit parameter explains exactly what you are adding to the the timestamp, thus making your code more easily understandable to other programmers who may be viewing it. It also takes care of the unit conversions behind the scenes, so there is less margin for human error and your code is not cluttered with math.

Major answered 28/3, 2023 at 12:58 Comment(0)
F
2

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 ) ;
Faber answered 28/3, 2023 at 21:19 Comment(2)
This was originally going to be my answer. I'm happy to see you posted it - I like using Duration :) To add to the latter half of your answer, you can also use ZonedDateTime, which may be easier for someone accustomed to using LocalDateTime.Major
@CardinalSystem I see your point. I added a brief mention of ZonedDateTime. Thanks.Faber

© 2022 - 2025 — McMap. All rights reserved.