How to convert a LocalDate to an Instant?

By | July 9, 2019

Question

I work with the new DateTime API of Java 8.

How to convert a LocalDate to an Instant?
I get an exception with

LocalDate date = LocalDate.of(2012, 2, 2);
Instant instant = Instant.from(date);

and I don’t understand why.

Solution

In order to convert it to an instant you need to have a LocalDateTime instance, e.g.:

LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC)

Rate this post

Leave a Reply

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