-->
Skip to main content

Java Timestamp to LocalDateTime Conversion [2021]

Java Timestamp to LocalDateTime Conversion 

Timestamp  to LocalDateTime Conversion can be done using toLocalDateTime() method provided by Timestamp class. In the earlier tutorial, we have seen how to Convert Java LocalDateTime to Timestamp.

Java Timestamp to LocalDateTime

In this tutorial, we will cover in deep about toLocalDateTime() method of Timestamp class and how we can convert Timestamp to LocalDateTime.

Java Timestamp to LocalDateTime Conversion

public LocalDateTime toLocalDateTime()

  • This method converts timestamp object to a LocalDateTime instance.
  • The conversion creates a LocalDateTime that represents the same year, month, day of month, hours, minutes, seconds and nanoseconds date-time value as this Timestamp in the local time zone.

Returns:

This returns LocalDateTime instance representing the same date-time value as this timestamp in the local time zone.

Since:

This method is since Java version 1.8.

Example:

 import java.sql.Timestamp;
 import java.time.LocalDateTime;

 public class TimeStampToLocalDateTime {
   public static void main(String[] args) {
     Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
     System.out.println("Timestamp: " + timestamp);
     //Timestamp: 2020-07-04 21:20:56.465

     // Converting Java Timestamp to LocalDateTime
     LocalDateTime localDateTime = timestamp.toLocalDateTime();

     System.out.println("LocalDateTime: " + localDateTime);
     //LocalDateTime: 2020-07-04T21:20:56.465
   }
 }

Output:

Timestamp: 2020-07-04 21:20:56.465
LocalDateTime: 2020-07-04T21:20:56.465

Conclusion:

In this tutorial, we have covered how to Convert Java Timestamp to LocalDateTime instance using the toLocalDateTime() method of Timestamp class.

Happy Learning!!!

Comments