Home Java LocalDateTime Methods minusSeconds() Java LocalDateTime minusSeconds() Method Definition In Java, minusSeconds(seconds) returns a new LocalDateTime that is the given number of seconds earlier than the original. The nanoseconds stay the same.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.minusSeconds(1);. When the seconds go back past the start of a minute, 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.minusSeconds(30); // 2026-03-15T10:29:30
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.minusSeconds(n)Returns a new date-time with n seconds subtracted. dt.plusSeconds(n)The same idea in the opposite direction. dt.minus(n, ChronoUnit.SECONDS)The general form. It does the same thing for this unit. time.minusSeconds(n)LocalTime has the same method, but it wraps around at midnight and has no date.
The result is a new object . Calling minusSeconds() without using the returned value does nothing useful.
Library declarations // java.time.LocalDateTime
public LocalDateTime minusSeconds(long seconds) Parameters Parameter Description seconds (long)The number of seconds to subtract. It can be zero or negative.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the seconds subtracted. The original is not changed.
Important
When the seconds and nanoseconds are both zero, toString() leaves the seconds out, so 10:30:00 is printed as 10:30. The value is 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. Look back 2. Rollover back 3. Zero seconds Example 1 — Look Back a Few Seconds Take the moment 10:30 and print the time 30 and 90 seconds earlier.
import java.time.LocalDateTime;
public class LdtMinusSecondsDemo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("now: " + now);
System.out.println("30 s ago: " + now.minusSeconds(30));
System.out.println("90 s ago: " + now.minusSeconds(90));
}
} now: 2026-03-15T10:30
30 s ago: 2026-03-15T10:29:30
90 s ago: 2026-03-15T10:28:30 How It Works The starting moment. 10:30 with zero seconds.30 seconds back. The minute goes back to 10:29 and the seconds show as :30.90 seconds back. That is one and a half minutes, so the result is 10:28:30.Example 2 — Go Back Across a Minute and a Year Subtract seconds from just after the hour, and from the first moment of a new year.
import java.time.LocalDateTime;
public class LdtMinusSecondsRollover {
public static void main(String[] args) {
LocalDateTime hour = LocalDateTime.of(2026, 3, 15, 10, 0, 10);
LocalDateTime year = LocalDateTime.of(2027, 1, 1, 0, 0, 0);
System.out.println("10:00:10 - 20s: " + hour.minusSeconds(20));
System.out.println("00:00:00 - 1s: " + year.minusSeconds(1));
}
} 10:00:10 - 20s: 2026-03-15T09:59:50
00:00:00 - 1s: 2026-12-31T23:59:59 How It Works 10:00:10 minus 20 seconds. The time goes back past the hour, so the result is 09:59:50.The first second of 2027. One second earlier is the last second of 2026.Date and year follow. The result is 31 December 2026 at 23:59:59.Example 3 — See Seconds Disappear from the Text Subtract 45 seconds from 10:30:45, and compare 3600 seconds with one hour.
import java.time.LocalDateTime;
public class LdtMinusSecondsHidden {
public static void main(String[] args) {
LocalDateTime base = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
System.out.println("before: " + base);
System.out.println("minus 45 s: " + base.minusSeconds(45));
System.out.println("3600 s = 1 h: " + base.minusSeconds(3600).equals(base.minusHours(1)));
}
} before: 2026-03-15T10:30:45
minus 45 s: 2026-03-15T10:30
3600 s = 1 h: true How It Works The starting moment. 10:30:45, so the seconds are printed.Subtract 45 seconds. That leaves exactly 10:30 and zero seconds.The text gets shorter. toString() hides zero seconds, so the result prints as 10:30.3600 seconds is one hour. minusSeconds(3600) equals minusHours(1), so the last line prints true.Frequently Asked Questions How do I subtract seconds from a LocalDateTime in Java? Call dt.minusSeconds(n). It returns a new LocalDateTime, so store it: dt = dt.minusSeconds(n).
Does minusSeconds() change the original LocalDateTime? No. LocalDateTime is immutable, so minusSeconds() returns a new object and leaves the original as it was.
Can I pass a negative number to minusSeconds()? Yes. A negative number moves the other way, so dt.minusSeconds(-n) gives the same result as dt.plusSeconds(n).
How do I get the time 10 seconds ago in Java? Use LocalDateTime.now().minusSeconds(10).
Why does my LocalDateTime lose its seconds when printed? 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.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion