Convert LocalDate to Instant in Java

Java LocalDate to Instant

In this article, we will see how to convert LocalDate to Instant in Java.

Java LocalDate to Instant

Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone.

Using toInstant() wth ZoneId

ZoneDateTime’s toInstant() method can be used to convert LocalDate to Instant. You need to provide ZoneId before doing the conversion.

  • Get LocalDate object with LocalDate.now()
  • Create ZoneId instance based on the Locale
  • Pass ZoneId to LocalDate‘s atStartOfDay() to get ZoneDateTime.
  • Use ZoneDateTime‘s toInstant() method to convert LocalDate to Instant in Java.

If you want to have default timezone, you can use ZoneId.systemDefault(). It will provide you default timezone of JVM.

Output:

Instant obj: 2022-02-16T18:30:00Z

Using toInstant() with ZoneOffset

You can pass ZoneOffset to ZoneDateTime’s toInstant() method to convert LocalDate to Instant in Java.

Output:

Instant obj: 2022-02-17T00:00:00Z

That’s all about how to convert LocalDate to Instant in Java.

Was this post helpful?

Leave a Reply

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