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

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 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 LocalTime to java.sql.Timestamp conversion, invoke atDate() method on the LocalTime object passing LocalDate as inputargument which will return LocalDateTime and pass converted LocalDateTime object as inputargument 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 –

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

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalTime to Calendar and vice-versa ?
Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?