Java LocalDateTime withSecond() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
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
CallWhat 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
// java.time.LocalDateTime
public LocalDateTime withSecond(int second)

Parameters

ParameterDescription
second (int)The new second, from 0 to 59.

Return Value

Return ValueDescription
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:

MethodHow it differs
with(field, value)The general method: you choose the field as an argument.
plusSeconds(n)Adds seconds to the current second instead of setting one.
withNano(n)Sets the fraction of a second.

Java Examples

Example 1 — Set and Clear the Seconds

Take 10:30 and set the second to 30, then set it back to 0.

Java
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);
    }
}

How It Works

  1. The start. 10:30 has zero seconds, so none are printed.
  2. Second 30. withSecond(30) gives 10:30:30, and the seconds now show.
  3. 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.

Java
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));
    }
}

How It Works

  1. A precise time. 10:30:45.500 has a half second.
  2. Seconds cleared. withSecond(0) gives 10:30:00.500, so the fraction is still there.
  3. 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.

Java
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));
    }
}

How It Works

  1. 60 is not allowed. Seconds go from 0 to 59, and LocalDateTime has no leap seconds.
  2. The exception is caught. withSecond(60) throws a DateTimeException.
  3. 59 is valid. The last line prints 10:30:59.

Frequently Asked Questions

Call dt.withSecond(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withSecond() returns a new object and leaves the original as it was.
A second below 0 or above 59 throws a DateTimeException. Java does not support leap seconds such as 60 here.
Use dt.withSecond(0).withNano(0), or dt.truncatedTo(ChronoUnit.MINUTES), which does both in one call.
No. The fraction of a second is kept. Use withNano(0) to clear it.

Next: withYear()

Return a copy with a different year.

withYear() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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