In this article, we will learn how to convert LocalTime to Sql time and vice-versa in Java 1.8 version
Conversion of LocalTime to SQL Time & vice-versa :
- Convert LocalTime to SQL Time using Time.valueOf() method
- Convert SQL Time to LocalTime using toLocalTime() method of SQL Time class
1. Convert LocalTime to SQL Time :
- LocalTime contains hour, minute, second and nanosecond parts in (HH:mm:ss.nnn) format
- But while converting LocalTime to SQL Time using Time.valueOf() method nanosecond part is left and thus SQL Time contains only hour, minute and second parts in (HH:mm:ss) format as shown in the below illustration
ConvertLocalTimeToJavaSqlTime.java
package in.bench.resources.java8.localtime.examples;
import java.sql.Time;
import java.time.LocalTime;
public class ConvertLocalTimeToJavaSqlTime {
public static void main(String[] args) {
// get current System Time
LocalTime localTime = LocalTime.now();
System.out.println("Current System time is :- \n"
+ localTime);
// convert LocalTime to java.sql.Time
Time time = Time.valueOf(localTime);
System.out.print("\nConversion of LocalTime to java.sql.Time \n"
+ time);
}
}
Output:
Current System time is :-
10:05:38.153248700
Conversion of LocalTime to java.sql.Time
10:05:38
2. Convert SQL Time to LocalTime :
- SQL Time class contains a method called toLocalTime() which converts SQL Time to LocalTime as shown in the below illustration
- Note: as SQL Time doesn’t poses nanosecond part so after conversion LocalTime won’t have nanosecond part as well
ConvertJavaSqlTimeToLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.sql.Time;
import java.time.LocalTime;
import java.util.Date;
public class ConvertJavaSqlTimeToLocalTime {
public static void main(String[] args) {
// get SQL time
Time sqlTime = new Time(new Date().getTime());
System.out.println("SQL Time is :- \n"
+ sqlTime);
// convert java.sql.Time to LocalTime
LocalTime localTime = sqlTime.toLocalTime();
System.out.print("\nConversion of java.sql.Time to LocalTime \n"
+ localTime);
}
}
Output:
SQL Time is :-
10:05:57
Conversion of java.sql.Time to LocalTime
10:05:57
Related Articles :
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Time and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Time.html
Happy Coding !!
Happy Learning !!