Convert Timestamp to LocalDate in Java [2021]
To Convert Timestamp to LocalDate in Java we have two strategies- Using Timestamp.toLocalDateTime() to convert first to LocalDateTime and then using LocalDateTime method toLocalDate() to convert to LocalDate.
- Using Timestamp.toInstant().atZone(ZoneId zone) to convert first to ZonedDateTime instance and then using toLocalDate() method of ZonedDateTime to get LocalDate.
we come across these cases usually where we need to convert timestamp to localdatetime, localdatetime to timestamp, localdate to localdatetime, localdatetime to date, etc. In this tutorial, we are covering a similar case to convert timestamp to localdate.
Contents
Here are the examples for 2 strategies
Timestamp to LocalDate
1. Using Timestamp.toLocalDateTime()
In this scenario, conversion takes place in two steps :
- Converting Timestamp to LocalDateTIme instance using Timestamp.toLocalDateTime() method.
- Converting LocalDateTime instance to LocalDate using LocalDateTime.toLocalDate() method.
//Creating timestamp instance
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
//2020-07-10 19:44:56.448
//Creating LocalDate using toLocalDateTime()
LocalDate localdate = timestamp.toLocalDateTime().toLocalDate();
System.out.println("Using toLocalDateTime() : " + localdate);
//Using toLocalDateTime() : 2020-07-10
2. Using Timestamp.toInstant().atZone(ZoneId zone)
In this scenario, conversion takes place in two steps :
- converting timestamp instance to ZonedDateTime instance using Timestamp.toInstant().atZone(ZoneId zone) method.
- converting ZonedDateTime instance to LocalDate instance using the toLocalDate() method of ZonedDateTime.
//Creating timestamp instance
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
//2020-07-10 19:44:56.448
//Creating LocalDate using toInstant().atZone()
LocalDate localdate = timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("Using toInstant.atZone() : " + localdate);
//Using toInstant.atZone() : 2020-07-10
One thing to highlight here that to convert java.sql.Timestamp to LocalDate in java we first have to convert Timestamp to either LocalDateTime or ZonedDateTime first then we get LocalDate instance.
Example:
Here is the full example to Convert Timestamp to LocalDate in Java.
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.ZoneId;
public class TimestampToLocalDate1 {
public static void main(String[] args) {
// Creating timestamp instance
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);
// 2020-07-10 19:44:56.448
// Creating LocalDate using toLocalDateTime()
LocalDate localdate1 = timestamp.toLocalDateTime().toLocalDate();
System.out.println("Using toLocalDateTime() : " + localdate1);
// Using toLocalDateTime() : 2020-07-10
// Creating LocalDate using toInstant().atZone()
LocalDate localdate2 = timestamp.toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("Using toInstant.atZone() : " + localdate2);
// Using toInstant.atZone() : 2020-07-10
}
}
Output:
2020-07-10 20:35:42.477
Using toLocalDateTime() : 2020-07-10
Using toInstant.atZone() : 2020-07-10
Conclusion:
In this tutorial, we have covered how to convert Timestamp to Localdate using 2 strategies
- By using timestamp.toLocalDateTime()
- By using timestamp.toInstant().atZone(ZoneId zone)
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.
Happy Learning!!!
More Examples you may like
Convert Instant to LocalDate in Java
Convert LocalDate to Date in Java
Convert Date to LocalDate in Java
Convert LocalDate to Date in Java
Convert Date to LocalDate in Java
Comments
Post a Comment
If you have any doubts, Please let me know.