Skip to content
Sharp

API Evolution · interview question

How would you convert a java.time.LocalDateTime to a java.time.Instant?

Multiple choiceHardchapter-12-curatedpremiumapi-evolutionmodern-date-and-time
Choose one answer
Answer and explanation

Correct answer

A. DateTime.atZone(ZoneId.systemDefault()).toInstant()

Why

LocalDateTime does not carry timezone information. To convert it to an Instant, you must first associate it with a timezone, typically the system's default, using atZone(), and then convert it to an Instant.

Test yourself on API Evolution

A free test with instant, explained results. No account needed.

Start free test
All API Evolution questions →