30

There is a whole set of date's classes in Java 8:

I already passed over their JavaDocs and paid attention that all these classes contain all the methods I need. Thus, for the moment, I can select them randomly. But I guess that there is some reason why there are 6 separate classes and each of them is dedicated to the specific purpose.

Technical information & requirements:

  1. The input is in String, which is converted to one of these date formats.
  2. I don't need to display the time zones but when I compare two dates it's important to be capable to compare correctly the time in New York and in Paris.
  3. The precise level is seconds, there is no need to use milliseconds.
  4. The required operations:
  • find max/min date;
  • sort objects by date;
  • calculate date & time period (difference between two dates);
  • insert objects to MongoDB and retrieve them from a db by date (e.g. all objects after specific date).

My questions:

  1. Which aspects should I bear in mind in order to choose the optimal format among these four options from the performance & maintainability points of view?

  2. Is there any reason why I should avoid some of these date classes?

Mike
  • 14,010
  • 29
  • 101
  • 161
  • 3
    I'm afraid this is too broad. You should start by reading the Javadoc for those classes. There is no good answer to this. – Tunaki Feb 09 '16 at 20:14
  • 2
    Don't use java.util.Date in Java 8. – keuleJ Feb 09 '16 at 20:16
  • Define "store a date", first; you mention `java.sql.Timestamp`, does that mean you intend to store into/retrieve from an RDBMS engine? Do you need timezone information? – fge Feb 09 '16 at 20:16
  • @Tunaki, I already passed over their JavaDocs and paid attention that all these classes contain all the methods I need, thus, for the moment I can select them randomly. But I guess, that there is some reason why there are 4 separate classes and each of them is dedicated to the specific purpose. – Mike Feb 09 '16 at 20:16
  • 1
    Well obviously i wouldn't use java.sql.Timestamp if you aren't planning to do JDBC. java.util.Date is pre-java 8 and notoriously clunky. I would go with Java 8's LocalDateTime. – Eric Guan Feb 09 '16 at 20:17
  • @EricGuan Unless timezones are important and `ZonedDateTime` should be preferred. – Tunaki Feb 09 '16 at 20:21
  • @fge, I want to save a date in object's property and then compare/sort objects by this value. Furthermore, I'll store these objects in `MongoDB` and will perform the queries by date field as well. – Mike Feb 09 '16 at 20:22
  • You have to give specific details. In what form do you get the input (string?), are time zones important, are they timestamps, what operations will be performed on them and what calculation will they be used for. – user1803551 Feb 09 '16 at 23:51
  • 2
    @user1803551, the question has been narrowed and enriched by technical details. – Mike Feb 10 '16 at 08:32
  • 2
    @MikeB. This may be informative: https://docs.oracle.com/javase/tutorial/datetime/iso/overview.html - what matters is the underlying data you are trying to describe. Your string only has date and time information - it is essentially a LocalDateTime. But then you also mention time zones. When you get that string, do you know in what time zone it is? Also note that mongodb represents dates as a number of milliseconds since the epoch - the closest representation in Java would be an Instant. You should give a bit more background as to what those date actually represent. – assylias Feb 10 '16 at 08:51

1 Answers1

23

Each one of the Date classes are for specific purposes:

  • If you want to use your Date in an SQL/JDBC context, use the java.sql.Timestamp.

  • java.util.Date is the old Java API, it is not thread safe, you can difficultly handle time zoning, and on the top of all, it is poorly designed: one simple uniformity is that months start from 1 while days start from 0.

  • java.time.LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second, which you need exactly.

  • java.time.ZonedDateTime class stores all date and time fields, so you can use it to deal with values like: 27th January 1990 at 15:40.30.123123123 +02:00 in the Europe/Paris time-zone.

To do your task, the ZonedDateTime class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant(which models a single instantaneous point on the time-line). The difference between the two time-lines, represented by a ZoneOffset, is the offset from UTC/Greenwich.

To calculate duration and period: there is the java.time.Duration which is a time-based amount of time, such as '20.5 seconds', and java.time.Period, which is a date-based amount of time (like: 26 years, 2 months and 2 days).

To get max and min dates, you can use the Java 8 lambdas in something like:

Date maxDate = list.stream().map(yourInstance -> yourInstance.date).max(Date::compareTo).get();
Date minDate = list.stream().map(yourInstance -> yourInstance.date).min(Date::compareTo).get();
Mike
  • 14,010
  • 29
  • 101
  • 161
Maouven
  • 330
  • 5
  • 11