How to convert a LocalDate to an Instant?

user1643352 picture user1643352 · Apr 22, 2014 · Viewed 88.2k times · Source

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.

Answer

mdo picture mdo · Apr 22, 2014

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

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