SteelReed

Search

Search posts, questions…

Java CoreQuestion 36 / 43

Junior

Why did java.time replace Date and Calendar? How do you use LocalDate, LocalDateTime and Instant?

Short answer

Date, Calendar, SimpleDateFormat can change and are unsafe across threads. java.time (Java 8) can't change, is safe and clear; new code uses only java.time.

setTime called on itmany requests parseplusDays returns newobjectDate passed elsewhereyour value changes toostatic SimpleDateFormatrandom wrong resultsjava.time: LocalDate, Instantsafe to share

3 points to say:

  1. A date without time uses LocalDate.
  2. An event moment like created_at uses Instant.
  3. A DateTimeFormatter can be a shared static final.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.