Home Java LocalDateTime Methods getSecond() Java LocalDateTime getSecond() Method 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.
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
int second = dt.getSecond(); // 45 Call What 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.time.LocalDateTime
public int getSecond() Parameters Parameter Description None getSecond() takes no parameters.
Return Value Return Value Description intThe second of the minute, from 0 to 59.
Compare with Similar Methods Other ways to read part of a date-time:
Java Examples 1. Read the second 2. Seconds since midnight 3. Tick and tock 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.
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());
}
} 2026-03-15T10:30:45 -> 45
2026-03-15T10:30 -> 0 How It Works A time with seconds. 10:30:45 gives 45.A time without seconds. 10:30 is printed without seconds because they are zero.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.
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);
}
} from the parts: 3723
from SECOND_OF_DAY: 3723 How It Works 01:02:03. One hour, two minutes, and three seconds.Do the math by hand. 3600 + 120 + 3 gives 3723 seconds.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.
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);
}
}
} second 0: tick
second 1: tock
second 2: tick
second 3: tock How It Works Four moments. The loop adds 0, 1, 2, and 3 seconds to 10:30:00.Read the second. getSecond() returns 0, 1, 2, and 3.Even or odd. % 2 == 0 picks "tick" for even seconds and "tock" for odd ones.Frequently Asked Questions How do I get the second from a LocalDateTime in Java? Call dt.getSecond(). It returns a number from 0 to 59.
Does getSecond() change the LocalDateTime? No. It only reads a value. LocalDateTime is immutable, so no method can change it.
How do I get the current second in Java? Use LocalDateTime.now().getSecond().
Why does my LocalDateTime not show seconds but getSecond() works? toString() hides seconds that are zero. getSecond() returns 0 in that case.
How do I get the milliseconds as well? Use getNano() / 1_000_000 for the milliseconds, or get(ChronoField.MILLI_OF_SECOND).
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