Each of the Date classes is designed for specific purposes:
If you want to use your date in the context of SQL / JDBC , use java.sql.Timestamp .
java.util.Date is an old Java API, it is not thread safe, you can hardly handle time zoning, and most importantly, it is poorly designed: one simple uniformity is that the months start at 1 and the days start at 0.
java.time.LocalDateTime is an immutable date object that represents a date-time, often seen as year-month-day-hour-minute-second, which you need for sure.
java.time.ZonedDateTime class stores all date and time fields, so you can use it to process values ββsuch as: 27th January 1990 at 15:40.30.123123123 +02:00 in the Europe / Paris time zone.
To accomplish its task, the ZonedDateTime class handles the conversion from the local LocalDateTime time line to the current Instant time line (which models one instantaneous point on the time line). The difference between the two timelines represented by a ZoneOffset is an offset from UTC / Greenwich.
To calculate the duration and period: there is java.time.Duration , which is a time interval such as "20.5 seconds" and java.time.Period , which is a time based on date (for example: 26 years, 2 months and 2 days).
To get the maximum and minimum dates, you can use Java 8 iambads 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();
Maouven
source share