1. Using ofInstant() method [Java 9+] [Recommended]
2. Using Java8 ZoneDateTime's toLocalDate() method
3. Using the Instant class toEpochMilli() method
Read Also: Instant to LocalDateTime in Java
Convert Instant to LocalDate in Java
In Java, a LocalDate represents a date without time or timezone information, whereas an Instant represents a specific moment in time in the UTC timezone.The task is to extract the date part from an Instant object and represent it as LocalDate.
1. Using ofInstant() method [Java 9+]
The easiest way to convert Instant to LocalDate in Java is using LocalDate's ofInstant() static method introduced in Java 9 as shown below in the example:import java.time.LocalDate; import java.time.Instant; import java.time.ZoneId; public class InstantToLocalDate { public static void main(String args[]) { // Create Instant class object Instant instant = Instant.now(); System.out.println("Current Instant is: " + instant); // Create ZoneId object ZoneId zoneId = ZoneId.of("America/Phoenix"); // Convert Instant to LocalDate using Java9 ofInstant() method LocalDate localDate = LocalDate.ofInstant(instant, zoneId); // Printing LocalDate System.out.println("LocalDate is: " + localDate); } }
Output:
Current Instant is: 2024-02-04T08:43:16.201354717Z
LocalDate is: 2024-02-04
2. Using Java8 ZoneDateTime's toLocalDate() method
There is another way to convert Instant to LocalDate in Java using Java8 ZoneDateTime's toLocalDate() method as shown below in the example:import java.time.ZoneId; import java.time.Instant; import java.time.LocalDate; public class InstantToLocalDate2 { public static void main(String args[]) { // Create Instant class object Instant instant = Instant.now(); System.out.println("Current Instant is: " + instant); // Convert Instant to LocalDate using Instant class atZone() method LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); // Printing LocalDate System.out.println("LocalDate is: " + localDate); } }
Output:
Current Instant is: 2024-02-04T10:20:10.975034364Z
LocalDate is: 2024-02-04
3. Using Instant class toEpochMilli() method
Another way to convert Instant to LocalDate in Java is by converting Instant to epoch milliseconds and then to LocalDate as shown below in the example:import java.util.Date; import java.time.LocalDate; import java.time.ZoneId; import java.time.Instant; public class InstantToLocalDate3 { public static void main(String args[]) { // Create Instant object using Instant.now() method Instant instant = Instant.now(); System.out.println("Current Instant is: " + instant); // Convert Instant to epochmilli and then to LocalDate() LocalDate localDate = new Date(instant.toEpochMilli()) .toInstant() .atZone(ZoneId.systemDefault()) .toLocalDate(); // Displaying LocalDate System.out.println("LocalDate is: " + localDate); } }
Output:
Current Instant is: 2024-02-04T10:36:39.739958946Z
LocalDate is: 2024-02-04
That's all for today. Please mention in the comments if you know any other way of converting Instant to LocalDate in Java.