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);
}
}
LocalTime: 10:20:30
Second of Day: 37230
Happy Coding 😊
Related Articles
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