Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?

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 inputargument 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
    1. LocalTime.MIDNIGHT
    2. LocalTime.now()
    3. LocalTime.NOON
    4. LocalTime.MIN
    5. 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,

  1. Convert Timestamp to LocalDate via LocalDateTime using toLocalDateTime() method
  2. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
Java 8 – How to convert LocalDate to an Instant ?