In this article, we will learn how to convert LocalDate to java.sql.Timestamp using atTime() method of LocalDate provided in Java 1.8 version and vice-versa
1. Convert LocalDate 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 LocalDate to java.sql.Timestamp conversion, first invoke atTime() method on the LocalDate object passing different LocalTime values like
- LocalTime.MIDNIGHT
- LocalTime.now()
- LocalTime.NOON
- LocalTime.MIN
- LocalTime.MAX
- Lets see an example for conversion of LocalDate to java.sql.Timestamp in the below illustration
ConvertLocalDateToJavaSqlTimestamp.java
package in.bench.resources.java8.localdate.examples;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalTime;
public class ConvertLocalDateToJavaSqlTimestamp {
public static void main(String[] args) {
// get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 1. convert LocalDate to Timestamp at Midnight
Timestamp timestamp1 = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT));
System.out.println("\n1. Conversion of LocalDate to Timestamp "
+ "using atTime(MIDNIGHT) is :- \n"
+ timestamp1);
// 2. convert LocalDate to Timestamp at current time
Timestamp timestamp2 = Timestamp.valueOf(localDate.atTime(LocalTime.now()));
System.out.println("\n2. Conversion of LocalDate to Timestamp "
+ "using atTime(now) is :- \n"
+ timestamp2);
// 3. convert LocalDate to Timestamp at NOON
Timestamp timestamp3 = Timestamp.valueOf(localDate.atTime(LocalTime.NOON));
System.out.println("\n3. Conversion of LocalDate to Timestamp "
+ "using atTime(NOON) is :- \n"
+ timestamp3);
// 4. convert LocalDate to Timestamp at MIN time
Timestamp timestamp4 = Timestamp.valueOf(localDate.atTime(LocalTime.MIN));
System.out.println("\n4. Conversion of LocalDate to Timestamp "
+ "using atTime(MIN) is :- \n"
+ timestamp4);
// 5. convert LocalDate to Timestamp at MAX time
Timestamp timestamp5 = Timestamp.valueOf(localDate.atTime(LocalTime.MAX));
System.out.print("\n5. Conversion of LocalDate to Timestamp "
+ "using atTime(MAX) is :- \n"
+ timestamp5);
}
}
Output:
Current System Date is :-
2022-08-02
1. Conversion of LocalDate to Timestamp using atTime(MIDNIGHT) is :-
2022-08-02 00:00:00.0
2. Conversion of LocalDate to Timestamp using atTime(now) is :-
2022-08-02 00:42:45.5668656
3. Conversion of LocalDate to Timestamp using atTime(NOON) is :-
2022-08-02 12:00:00.0
4. Conversion of LocalDate to Timestamp using atTime(MIN) is :-
2022-08-02 00:00:00.0
5. Conversion of LocalDate to Timestamp using atTime(MAX) is :-
2022-08-02 23:59:59.999999999
2. Convert java.sql.Timestamp to LocalDate :
There are 2 ways to convert java.sql.Timestamp to LocalDate, those options are,
- Convert Timestamp to LocalDate via LocalDateTime using toLocalDateTime() method
- Convert Timestamp to LocalDate via Instant using toInstant() method
2.1 Timestamp to LocalDate via LocalDateTime :
- First, convert Timestamp to LocalDateTime using toLocalDateTime() method and then invoke toLocalDate() method which returns LocalDate
- In Short, Timestamp -> LocalDateTime -> LocalDate
ConvertJavaSqlTimestampToLocalDate1.java
package in.bench.resources.java8.localdate.examples;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertJavaSqlTimestampToLocalDate1 {
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. Finally, convert java.sql.Timestamp to LocalDate via LocalDateTime
LocalDate localDate = localDateTime.toLocalDate();
System.out.print("\nConversion of java.sql.Timestamp to LocalDate via LocalDateTime is :- \n"
+ localDate);
}
}
Output:
Current Timestamp is :-
2022-08-02 00:53:20.999
Conversion of java.sql.Timestamp to LocalDate via LocalDateTime is :-
2022-08-02
2.2 Timestamp to LocalDate via Instant :
- First, convert Timestamp to Instant using toInstant() method and then
- Add zone information using atZone() method passing ZoneId as argument
- And invoke toLocalDate() method which returns LocalDate
- In Short, Timestamp -> Instant -> ZonedDateTime -> LocalDate
ConvertJavaSqlTimestampToLocalDate2.java
package in.bench.resources.java8.localdate.examples;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class ConvertJavaSqlTimestampToLocalDate2 {
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();
// 3. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nSystem default Zone is :- \n" + zoneId);
// 3. Finally, convert java.sql.Timestamp to LocalDate via Instant
LocalDate localDate = instant.atZone(zoneId).toLocalDate();
System.out.print("\nConversion of java.sql.Timestamp to LocalDate via Instant is :- \n"
+ localDate);
}
}
Output:
Current Timestamp is :-
2022-08-02 00:53:35.789
System default Zone is :-
Asia/Calcutta
Conversion of java.sql.Timestamp to LocalDate via Instant is :-
2022-08-02
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- 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/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 !!