Date to LocalDate Java Conversion[2021]
Date to LocalDate Java Conversion can be done using 3 ways, There are many cases where we need to convert Date to LocalDate in our projects to make new API and old API Compatible. In the earlier tutorial, we had seen how to Convert LocalDate to Date in Java.
Here are the 3 ways to Convert Date to LocalDate Java instance
- By creating Instant from Date with the date.toInstant() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance.
- By creating Instant from Instant.ofEpochMilli() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance.
- By Creating java.sql.Date new Constructor with java.sql.Date class toLocalDate() method to get LocalDate instance.
Let's see each way one by one
Date to LocalDate Java
1. By creating Instant from Date with the date.toInstant() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance.Example:
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class DateToLocalDateEx1 {
public static void main(String[] args) {
// Creating Date instance
Date date = new Date();
// Creating Instant instance
Instant instant = date.toInstant();
// Creating ZonedDateTime instance
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
// Creating LocalDate instance
LocalDate localDate = zdt.toLocalDate();
System.out.println("Date to LocalDate : " + localDate);
}
}
Output:
Date to LocalDate : 2020-07-06
Here we are converting Date to LocalDate with 3 steps.
- First creating Instant instance from java.util.Date instance using date.toInstant() method.
- Second, we are creating ZonedDateTime instance using an instant instance with atZone() method with system default ZoneId.
- Finally in Third Step, we are creating LocalDate instance from ZonedDateTime instance using toLocalDate() method.
2. By creating Instant from Instant.ofEpochMilli() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance.
Example:
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class DateToLocalDateEx2 {
public static void main(String[] args) {
// Creating Date instance
Date date = new Date();
// Creating Instant instance using ofEpcohMilli method
Instant instant = Instant.ofEpochMilli(date.getTime());
// Creating ZonedDateTime instance
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
// Creating localdate instance
LocalDate localdate = zdt.toLocalDate();
System.out.println("Date to LocalDate : " + localdate);
}
}
Output:
Date to LocalDate : 2020-07-06
Here we are converting Date to LocalDate with 3 steps.
- First by creating Instant from date instance but this time using Instant.ofEpochMilli() method of Instant class with date instance.
- Secondly by creating ZonedDateTIme instance using instant instance created with atZone() method with system default ZoneId.
- Finally, we are creating a LocalDate instance from ZonedDateTime instance using toLocalDate() method.
3. By Creating java.sql.Date new Constructor with java.sql.Date class toLocalDate() method to get LocalDate instance.
Example:
import java.time.LocalDate;
import java.util.Date;
public class DateToLocalDateEx3 {
public static void main(String[] args) {
//Creating Date instance
Date date = new Date();
//Creating LocalDate instance using sql Date class toLocalDate method.
LocalDate localDate = new java.sql.Date(date.getTime()).toLocalDate();
System.out.println("Date to LocalDate : " + localDate);
}
}
Output:
Date to LocalDate : 2020-07-06
Here we are converting Date to LocalDate with 1 step.
- By using java.sql.Date Constuctor with date.getTime() as parameter and then using sql Date class toLocalDate() method on sql date instance to return LocalDate instance.
Conclusion:
In this tutorial, we have covered how to convert Date to LocalDate Java using three techniques
- By creating Instant from Date with the date.toInstant() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance.
- By creating Instant from Instant.ofEpochMilli() method and then using ZonedDateTime.toLocalDate() method to get LocalDate instance.
- By Creating java.sql.Date new Constructor with java.sql.Date class toLocalDate() method to get LocalDate instance.
Happy Learning!!!
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.