Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Java 8 — Optional, Date/Time API, Method Refs

Why is the old SimpleDateFormat dangerous, and what replaced it?

Level II · JuniorJava 8 — Optional, Date/Time API, Method Refs
Short answer

SimpleDateFormat is mutable and not thread-safe, yet developers routinely cached one in a static field and shared it across threads, producing intermittent parse corruption and wrong dates under load — a notorious heisenbug.

Explanation

The java.time replacement, DateTimeFormatter, is immutable and thread-safe, so a single instance can be shared as a constant safely. It also has predefined ISO formatters and a clearer pattern/builder API, removing both the concurrency hazard and much of the old format-string ambiguity.

Common mistakes
  • Sharing a SimpleDateFormat between threads.
  • Not using the immutable, thread-safe DateTimeFormatter.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Java 8 — Optional, Date/Time API, Method Refs.

Practise free

More on Java 8 — Optional, Date/Time API, Method Refs