2

We use new Date(), Calendar().getInstance() or LocalDateTime.now() to represent the clock now. What's the difference between LocalDateTime, Date and Calendar and why do we prefer LocalDateTime instead of the rest?

  • 2
    Use the newer classes, they're much more useful and better designed. If you're seeing a difference, please post a [mcve]. – shmosel Dec 06 '18 at 02:09
  • Because the original `Date` and `Calendar` classes leave much to be desired. Which led to [Joda-Time](https://www.joda.org/joda-time/), and eventually `java.time` – Elliott Frisch Dec 06 '18 at 02:10
  • There are only a few instances where Date is more useful than Instant, mostly related to historical dates. – Compass Dec 06 '18 at 03:28
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 06 '18 at 05:49

0 Answers0