1. Using LocalDate class atTime() method
2. Using LocalDate class atStartOfDay() method
3. Using LocalDateTime.of() method
Read Also: Convert Instant to LocalDate in Java
Convert LocalDate to Instant in Java
Converting LocalDate to Instant in Java usually is a two-step process. First, converting LocalDate into ZonedDateTime or Timestamp and then calling their toInstant() method we get Instant.1. Using LocalDate class atTime() method
We can easily convert LocalDate to Instant using the LocalDate class atTime() method as shown below in the example:import java.time.LocalDate; import java.time.ZoneId; import java.time.Instant; import java.time.LocalTime; public class LocalDateToInstant { public static void main(String args[]) { LocalDate localDate = LocalDate.parse("2024-03-04"); System.out.println("Current LocalDate is: " + localDate); ZoneId zoneId = ZoneId.systemDefault(); Instant instant = localDate.atTime(LocalTime.NOON).atZone(zoneId).toInstant(); System.out.println("Instant is: " + instant); } }
Output:
Current LocalDate is: 2024-03-04
Instant is: 2024-03-04T12:00:00Z
2. Using LocalDate class atStartOfDay() method
There is another way to convert LocalDate to Instant using the LocalDate class atStartOfDay() method as shown below in the example:import java.time.LocalDate; import java.time.ZoneId; import java.time.Instant; import java.time.LocalTime; public class LocalDateToInstant2 { public static void main(String args[]) { LocalDate localDate = LocalDate.parse("2024-03-04"); System.out.println("LocalDate is: " + localDate); ZoneId zoneId = ZoneId.systemDefault(); Instant instant = localDate.atStartOfDay(zoneId).toInstant(); System.out.println("Instant is: " + instant); } }
Output:
LocalDate is: 2024-03-04
Instant is: 2024-03-04T00:00:00Z
3. Using LocalDateTime.of() method
Another way to convert LocalDate to Instant in Java is by converting LocalDate to LocalDateTime and then to Instant as shown below in the example:import java.time.LocalDate; import java.time.ZoneId; import java.time.Instant; import java.time.LocalDateTime; import java.time.LocalTime; public class LocalDateToInstant3 { public static void main(String args[]) { LocalDate localDate = LocalDate.parse("2024-03-04"); System.out.println("LocalDate is: " + localDate); ZoneId zoneId = ZoneId.systemDefault(); Instant instant = LocalDateTime.of(localDate, LocalTime.NOON).atZone(zoneId).toInstant(); System.out.println("Instant is: " + instant); } }
Output:
LocalDate is: 2024-03-04
Instant is: 2024-03-04T12:00:00Z
That's all for today. Please mention in the comments if you know any other way of converting LocalDate to Instant in Java.