Java Convert LocalTime to SQL Time

Tags: Java LocalTime Java SQL Time Java 8

In this Java core tutorial we learn how to convert a java.time.LocalTime object to a java.sql.Time in Java programming language.

How to convert LocalTime to SQL Time in Java

In Java, with a given LocalTime object we can use the Time.valueOf(LocalTime time) static method to convert it to a Time object as the Example Java code below.

ConvertLocalTimeToTimeExample1.java

import java.sql.Time;
import java.time.LocalTime;

public class ConvertLocalTimeToTimeExample1 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.now();

        // Convert LocalTime object to Time object
        Time time = Time.valueOf(localTime);

        System.out.println("LocalTime: " + localTime);
        System.out.println("Time: " + time);
    }
}
The output as below.
LocalTime: 21:44:58.179410800
Time: 21:44:58

Happy Coding 😊

Java Convert SQL Time to LocalTime