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


import_contacts

You may also like:

Related Posts

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which we can from Zone. Here is […]

  • 18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • 26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-60081f9797742369151099/] Create a list of movies and convert with to […]

  • 29 February

    Java long to String

    In this post, we will see how to convert long to String in java. There are lot of ways to convert long to String.Let’s see each one by one. Using Long.toString() You can use Long class toString() method to convert long to String. [crayon-60081f97bc458864658770/] In case, Long can be null and you don’t want to […]

  • 15 May

    Java LocalDateTime to Date

    In this post, we will see how to convert LocalDateTime to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDateTime to date using Instant object which we can from ZonedDateTime. Here is […]

  • 20 October

    Java Array to Set

    In this post, we will learn java array to set conversion. There are many ways to convert array to set. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using this method. [crayon-60081f97bcd1e138378535/] Output [John, Martin, Mary] 2. Using HashSet constructor() We can directly call HashSet‘s constructor for java set […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.