Java LocalDateTime getSecond() Method

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

Definition

In Java, getSecond() returns the second part of a LocalDateTime, from 0 to 59.

It does not include the fraction of a second. For that, use getNano().

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. It takes no arguments.

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
int second = dt.getSecond();   // 45
CallWhat it does
dt.getSecond()Returns the second from 0 to 59, for example 45.
dt.getMinute()Returns the minute of the hour, 0 to 59.
dt.getNano()Returns the fraction of the second, in nanoseconds.
dt.get(ChronoField.SECOND_OF_DAY)Returns the seconds since midnight, 0 to 86399.

A time printed as 10:30 has getSecond() equal to 0. Printing only hides the zero seconds.

Library declarations
Java
// java.time.LocalDateTime
public int getSecond()

Parameters

ParameterDescription
NonegetSecond() takes no parameters.

Return Value

Return ValueDescription
intThe second of the minute, from 0 to 59.

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getMinute()Reads the minute.
getNano()Reads the fraction of the second.
withSecond(s)Sets the second instead of reading it.

Java Examples

Example 1 — Read the Seconds, Even When Hidden

Read the second of a time with seconds and of a time whose seconds are hidden when printed.

Java
import java.time.LocalDateTime;

public class LdtGetSecondDemo {
    public static void main(String[] args) {
        LocalDateTime withSeconds = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
        LocalDateTime noSeconds = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println(withSeconds + " -> " + withSeconds.getSecond());
        System.out.println(noSeconds + " -> " + noSeconds.getSecond());
    }
}

How It Works

  1. A time with seconds. 10:30:45 gives 45.
  2. A time without seconds. 10:30 is printed without seconds because they are zero.
  3. The value is still there. getSecond() returns 0 for the second time, so the field exists even when it is hidden.

Example 2 — Count Seconds Since Midnight

Combine the hour, minute, and second to count the seconds since midnight.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

public class LdtGetSecondMidnight {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 1, 2, 3);

        int fromParts = dt.getHour() * 3600 + dt.getMinute() * 60 + dt.getSecond();
        int fromField = dt.get(ChronoField.SECOND_OF_DAY);

        System.out.println("from the parts:      " + fromParts);
        System.out.println("from SECOND_OF_DAY:  " + fromField);
    }
}

How It Works

  1. 01:02:03. One hour, two minutes, and three seconds.
  2. Do the math by hand. 3600 + 120 + 3 gives 3723 seconds.
  3. Ask for the field. ChronoField.SECOND_OF_DAY gives the same 3723.

Example 3 — Alternate Between Tick and Tock

Move forward one second at a time and say "tick" for even seconds and "tock" for odd ones.

Java
import java.time.LocalDateTime;

public class LdtGetSecondTickTock {
    public static void main(String[] args) {
        LocalDateTime base = LocalDateTime.of(2026, 3, 15, 10, 30, 0);

        for (int i = 0; i < 4; i++) {
            LocalDateTime t = base.plusSeconds(i);
            String sound = t.getSecond() % 2 == 0 ? "tick" : "tock";
            System.out.println("second " + t.getSecond() + ": " + sound);
        }
    }
}

How It Works

  1. Four moments. The loop adds 0, 1, 2, and 3 seconds to 10:30:00.
  2. Read the second. getSecond() returns 0, 1, 2, and 3.
  3. Even or odd. % 2 == 0 picks "tick" for even seconds and "tock" for odd ones.

Frequently Asked Questions

Call dt.getSecond(). It returns a number from 0 to 59.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Use LocalDateTime.now().getSecond().
toString() hides seconds that are zero. getSecond() returns 0 in that case.
Use getNano() / 1_000_000 for the milliseconds, or get(ChronoField.MILLI_OF_SECOND).

Next: getYear()

Read the year as a number.

getYear() 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.

14 people found this page helpful