Java Convert LocalTime to Seconds of Day

Tags: Java LocalTime Java 8

In this Java core tutorial we learn how to convert a java.time.LocalTime object into a number of seconds of day in Java programming language.

How to convert LocalTime to Seconds of Day

With a given LocalTime object we can use the toSecondOfDay() method to extract the time as seconds of day. In the following Java program we learn how to use the LocalTime.toSecondOfDay() method .

ConvertLocalTimeToSecondOfDayExample1.java

import java.time.LocalTime;

public class ConvertLocalTimeToSecondOfDayExample1 {
    public static void main(String... args) {
        int hour = 10;
        int minute = 20;
        int second = 30;

        LocalTime localTime = LocalTime.of(hour, minute, second);
        int secondOfDay = localTime.toSecondOfDay();

        System.out.println("LocalTime: " + localTime);
        System.out.println("Second of Day: " + secondOfDay);
    }
}
The output as below.
LocalTime: 10:20:30
Second of Day: 37230

Happy Coding 😊

Java Convert LocalTime to String

Java Convert LocalTime to Nanos of Day

Java Convert LocalTime to Epoch Seconds

Java Convert LocalTime to LocalDateTime

Java Convert LocalTime to OffsetTime

Java Convert LocalTime to Date

Java Convert LocalTime to Calendar

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples