Java Date to LocalDate

Convert Date to LocalDate in java

In this post, we will see how to convert Date to LocalDate in java.

Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java.

Using toInstant() method of Date class

You can convert Date to LocalDate using toInstant() method of Date class. Since Instant objects are time agnostic, you need to use atZone() method to convert to derive LocalDate from it.
Here is an example:

Output:

Local Date: 2021-01-07

Using toInstant() method of Date class

You can convert Date to LocalDate using toInstant() method of Date class. You can use Intant.ofEpochMilli(date.getTime()) to derive instant object and use atZone() method to associate time zone to instant object.
Here is an example:

Output:

Local Date: 2021-01-07

Using java.sql.Date

You can convert Date to LocalDate using java.sql.Date. In Java 8, there is new method toLocalDate() added to java.sql.Date class.`
Here is an example:

Output:

Local Date: 2021-01-07

That’s all about how to convert Date to LocalDate in java

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *