Home Java LocalDateTime Methods plusSeconds() Java LocalDateTime plusSeconds() Method Definition In Java, plusSeconds(seconds) returns a new LocalDateTime that is the given number of seconds later than the original. The nanoseconds stay the same.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.plusSeconds(1);. When the seconds pass 60, the minute changes, and the hour and the date follow when needed.
An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file.
Syntax Call it on a LocalDateTime and pass the number of seconds. A negative number goes the other way.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.plusSeconds(45); // 2026-03-15T10:30:45
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.plusSeconds(n)Returns a new date-time with n seconds added. dt.minusSeconds(n)The same idea in the opposite direction. dt.plus(n, ChronoUnit.SECONDS)The general form. It does the same thing for this unit. time.plusSeconds(n)LocalTime has the same method, but it wraps around at midnight and has no date.
The result is a new object . Calling plusSeconds() without using the returned value does nothing useful.
Library declarations // java.time.LocalDateTime
public LocalDateTime plusSeconds(long seconds) Parameters Parameter Description seconds (long)The number of seconds to add. It can be zero or negative.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the seconds added. The original is not changed.
Important
When the seconds and nanoseconds are both zero, toString() leaves the seconds out, so 10:31:00 is printed as 10:31. The value is still the same: only the text is shorter. Use format() with a pattern if you always want seconds.
Compare with Similar Methods Methods that move a date-time in time:
Java Examples 1. Timeout 2. Zero seconds 3. Rollover Example 1 — Work Out a Timeout A login code is created at 10:30 and expires 45 seconds later. Print both moments.
import java.time.LocalDateTime;
public class LdtPlusSecondsDemo {
public static void main(String[] args) {
LocalDateTime created = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime expires = created.plusSeconds(45);
System.out.println("created: " + created);
System.out.println("expires: " + expires);
}
} created: 2026-03-15T10:30
expires: 2026-03-15T10:30:45 How It Works The creation time. 10:30 on 15 March 2026, with zero seconds.Add 45 seconds. plusSeconds(45) gives 10:30:45.Seconds appear when they matter. The first line shows no seconds because they are zero, and the second line shows :45.Example 2 — See When Seconds Are Hidden Add 0, 60, and 61 seconds and watch how the printed text changes.
import java.time.LocalDateTime;
public class LdtPlusSecondsHidden {
public static void main(String[] args) {
LocalDateTime base = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("+0 seconds: " + base.plusSeconds(0));
System.out.println("+60 seconds: " + base.plusSeconds(60));
System.out.println("+61 seconds: " + base.plusSeconds(61));
}
} +0 seconds: 2026-03-15T10:30
+60 seconds: 2026-03-15T10:31
+61 seconds: 2026-03-15T10:31:01 How It Works Adding zero. The result equals the original and prints without seconds.60 seconds is one minute. The minute changes to 31 and the seconds are zero again, so they stay hidden.61 seconds. One minute and one second, so the text shows 10:31:01.Example 3 — Add Seconds Across a Minute and a Year Roll over to the next minute, over the end of the year, and compare 3600 seconds with one hour.
import java.time.LocalDateTime;
public class LdtPlusSecondsRollover {
public static void main(String[] args) {
LocalDateTime a = LocalDateTime.of(2026, 3, 15, 10, 59, 50);
LocalDateTime b = LocalDateTime.of(2026, 12, 31, 23, 59, 59);
System.out.println("10:59:50 + 20s: " + a.plusSeconds(20));
System.out.println("23:59:59 + 1s: " + b.plusSeconds(1));
System.out.println("3600s equals 1h: " + a.plusSeconds(3600).equals(a.plusHours(1)));
}
} 10:59:50 + 20s: 2026-03-15T11:00:10
23:59:59 + 1s: 2027-01-01T00:00
3600s equals 1h: true How It Works 10:59:50 plus 20 seconds. The minute and the hour both roll over, giving 11:00:10.The last second of the year. One more second is midnight on 1 January 2027, which prints as 00:00.3600 seconds is one hour. plusSeconds(3600) and plusHours(1) give equal results, so the last line prints true.Frequently Asked Questions How do I add seconds to a LocalDateTime in Java? Call dt.plusSeconds(n). It returns a new LocalDateTime, so store it: dt = dt.plusSeconds(n).
Does plusSeconds() change the original LocalDateTime? No. LocalDateTime is immutable, so plusSeconds() returns a new object and leaves the original as it was.
Can I pass a negative number to plusSeconds()? Yes. A negative number moves the other way, so dt.plusSeconds(-n) gives the same result as dt.minusSeconds(n).
Why does my LocalDateTime print without seconds? toString() leaves out the seconds when they are zero. The value is the same; use format() with the pattern HH:mm:ss to always show them.
How do I add 1 hour using seconds? Call plusSeconds(3600). It gives the same result as plusHours(1).
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion