Home Java LocalDateTime Methods withSecond() Java LocalDateTime withSecond() Method Definition In Java, withSecond(second) returns a new LocalDateTime with the second set to the value you pass. The date, the hour, the minute, and the nanoseconds are copied from the original.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.withSecond(30);. The minute never changes, even when you set 0 or 59 seconds.
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 new second.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withSecond(30); // 2026-03-15T10:30:30
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.withSecond(n)Returns a copy with the second replaced by n. dt.with(ChronoField.SECOND_OF_MINUTE, n)The general form for any field. It does the same thing here. dt.plusSeconds(n)Moves by an amount instead of setting a value. time.withSecond(s)LocalTime has the same method, but it has no date.
The result is a new object . Everything except the second is copied from the original.
Library declarations // java.time.LocalDateTime
public LocalDateTime withSecond(int second) Parameters Parameter Description second (int)The new second, from 0 to 59.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the second replaced. The original is not changed.
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Set the seconds 2. Fraction stays 3. Invalid second Example 1 — Set and Clear the Seconds Take 10:30 and set the second to 30, then set it back to 0.
import java.time.LocalDateTime;
public class LdtWithSecondDemo {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime withSeconds = start.withSecond(30);
LocalDateTime back = withSeconds.withSecond(0);
System.out.println("start: " + start);
System.out.println("second 30: " + withSeconds);
System.out.println("second 0: " + back);
}
} start: 2026-03-15T10:30
second 30: 2026-03-15T10:30:30
second 0: 2026-03-15T10:30 How It Works The start. 10:30 has zero seconds, so none are printed.Second 30. withSecond(30) gives 10:30:30, and the seconds now show.Back to 0. The seconds are zero again, so the text goes back to 10:30.Example 2 — See That the Fraction Stays Start at 10:30:45.500, set the second to 0, and then clear the fraction too.
import java.time.LocalDateTime;
public class LdtWithSecondFraction {
public static void main(String[] args) {
LocalDateTime precise = LocalDateTime.of(2026, 3, 15, 10, 30, 45, 500_000_000);
System.out.println("start: " + precise);
System.out.println("withSecond(0): " + precise.withSecond(0));
System.out.println("and withNano(0): " + precise.withSecond(0).withNano(0));
}
} start: 2026-03-15T10:30:45.500
withSecond(0): 2026-03-15T10:30:00.500
and withNano(0): 2026-03-15T10:30 How It Works A precise time. 10:30:45.500 has a half second.Seconds cleared. withSecond(0) gives 10:30:00.500, so the fraction is still there.Clear the fraction too. withNano(0) removes it, leaving exactly 10:30.Example 3 — Try a Second That Does Not Exist Set the second to 60 and to 59.
import java.time.DateTimeException;
import java.time.LocalDateTime;
public class LdtWithSecondInvalid {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
try {
start.withSecond(60);
} catch (DateTimeException e) {
System.out.println("withSecond(60) -> DateTimeException");
}
System.out.println("withSecond(59) -> " + start.withSecond(59));
}
} withSecond(60) -> DateTimeException
withSecond(59) -> 2026-03-15T10:30:59 How It Works 60 is not allowed. Seconds go from 0 to 59, and LocalDateTime has no leap seconds.The exception is caught. withSecond(60) throws a DateTimeException.59 is valid. The last line prints 10:30:59.Frequently Asked Questions How do I change only the second of a LocalDateTime in Java? Call dt.withSecond(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withSecond() change the original LocalDateTime? No. LocalDateTime is immutable, so withSecond() returns a new object and leaves the original as it was.
What happens if the value passed to withSecond() is not valid? A second below 0 or above 59 throws a DateTimeException. Java does not support leap seconds such as 60 here.
How do I clear the seconds of a LocalDateTime in Java? Use dt.withSecond(0).withNano(0), or dt.truncatedTo(ChronoUnit.MINUTES), which does both in one call.
Does withSecond() also change the milliseconds? No. The fraction of a second is kept. Use withNano(0) to clear it.
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.
13 people found this page helpful
Helpful Share Copy link Suggestion