In this article, we will learn how to convert LocalTime to java.sql.Timestamp using atDate() method of LocalTime provided in Java 1.8 version and vice-versa
1. Convert LocalTime to java.sql.Timestamp :
- Timestamp.valueOf() method accepts LocalDateTime as input–argument and returns Timestamp
- valueOf(LocalDateTime) – Obtains an instance of Timestamp from a LocalDateTime object, with the same year, month, day of month, hours, minutes, seconds and nanos date-time value as the provided LocalDateTime
- valueOf() method is static so there is no need to create/instantiate an object of Timestamp for this conversion
- For LocalTime to java.sql.Timestamp conversion, invoke atDate() method on the LocalTime object passing LocalDate as input–argument which will return LocalDateTime and pass converted LocalDateTime object as input–argument to Timestamp.valueOf() method
- Lets see an example for conversion of LocalTime to java.sql.Timestamp in the below illustration
ConvertLocalTimeToJavaSqlTimestamp.java
package in.bench.resources.java8.localtime.examples;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalTime;
public class ConvertLocalTimeToJavaSqlTimestamp {
public static void main(String[] args) {
// get current System Date
LocalTime localTime = LocalTime.now();
System.out.println("Current System time is :- \n"
+ localTime);
// get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("\nCurrent System date is :- \n"
+ localDate);
// convert LocalTime to Timestamp
Timestamp timestamp = Timestamp.valueOf(localTime.atDate(localDate));
System.out.print("\nConversion of LocalTime to Timestamp "
+ "using atDate() is :- \n"
+ timestamp);
}
}
Output:
Current System time is :-
19:44:31.699073600
Current System date is :-
2022-08-25
Conversion of LocalTime to Timestamp using atDate() is :-
2022-08-25 19:44:31.6990736
2. Convert java.sql.Timestamp to LocalTime :
There are 2 ways to convert java.sql.Timestamp to LocalTime, those options are –
- Convert Timestamp to LocalTime via LocalDateTime using toLocalDateTime() method
- Convert Timestamp to LocalDate via Instant/ZonedDateTime using toInstant() & atZone() methods
2.1 Timestamp to LocalTime via LocalDateTime :
- First, convert Timestamp to LocalDateTime using toLocalDateTime() method and then invoke toLocalTime() method which returns LocalTime
- In Short, Timestamp -> LocalDateTime -> LocalTime
ConvertJavaSqlTimestampToLocalTime1.java
package in.bench.resources.java8.localtime.examples;
import java.sql.Timestamp;
import java.time.LocalTime;
import java.time.LocalDateTime;
public class ConvertJavaSqlTimestampToLocalTime1 {
public static void main(String[] args) {
// 1. get current java.sql.Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("Current Timestamp is :- \n" + timestamp);
// 2. First, convert java.sql.Timestamp to LocalDateTime
LocalDateTime localDateTime = timestamp.toLocalDateTime();
// 3. convert java.sql.Timestamp -> LocalDateTime -> LocalTime
LocalTime localTime = localDateTime.toLocalTime();
System.out.print("\nConversion of java.sql.Timestamp to LocalTime"
+ " via LocalDateTime is :- \n"
+ localTime);
}
}
Output:
Current Timestamp is :-
2022-08-25 19:47:05.904
Conversion of java.sql.Timestamp to LocalTime via LocalDateTime is :-
19:47:05.904
2.2 Timestamp to LocalTime via Instant/ZonedDateTime :
- First, convert Timestamp to Instant using toInstant() method and then
- Add zone information using atZone() method passing ZoneId as argument
- And invoke toLocalTime() method which returns LocalTime
- In Short, Timestamp -> Instant -> ZonedDateTime -> LocalTime
ConvertJavaSqlTimestampToLocalTime2.java
package in.bench.resources.java8.localtime.examples;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
public class ConvertJavaSqlTimestampToLocalTime2 {
public static void main(String[] args) {
// 1. get current java.sql.Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("Current Timestamp is :- \n" + timestamp);
// 2. First, convert java.sql.Timestamp to Instant
Instant instant = timestamp.toInstant();
System.out.println("\nCurrent Instant at GMT/UTC is :- \n"
+ instant);
// 3. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nSystem default Zone is :- \n"
+ zoneId);
// 4. convert java.sql.Timestamp -> Instant -> ZonedDateTime -> LocalTime
LocalTime localTime = instant // Instant
.atZone(zoneId) // ZonedDateTime
.toLocalTime(); // LocalTime
System.out.print("\nConversion of java.sql.Timestamp to LocalTime"
+ " via Instant/ZonedDateTime is :- \n"
+ localTime);
}
}
Output:
Current Timestamp is :-
2022-08-25 19:48:11.478
Current Instant at GMT/UTC is :-
2022-08-25T14:18:11.478Z
System default Zone is :-
Asia/Calcutta
Conversion of java.sql.Timestamp to LocalTime via Instant/ZonedDateTime is :-
19:48:11.478
Related Articles:
- Java 8 – LocalTime with method details and examples
- 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 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 ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!