Never use a long
A long
or Long
exposes internal implementation details that could change. Some utilities and libraries track date-time by seconds, microseconds, or nanoseconds rather than the milliseconds you might assume.
The count is from an epoch reference date-time. There are dozens of such epochs in use by various software systems. So which epoch can be ambiguous.
Working with date-time values as a number from epoch is inherently confusing as they are not human readable. So debugging and testing are complicated.
Why not track text as a series of octets or even bits? Because we want libraries to handle the nitty-gritty complexities and chores (UTF-8, MacRoman, searching, replacing, trimming, etc.) for us. Likewise, date-time values should be handled as date-time data types.
If given a count of seconds or milliseconds since epoch of first moment of 1970 in UTC, 1970-01-01T00:00Z, parse into Instant
using convenient factory methods found on that class.
Never use Date
or Calendar
That said, the java.util.Date and .Calendar classes are terrible. Avoid them whenever possible.
Always use java.time
With Java 8 and later, we have the modern java.time package. These supplanted the terrible old legacy classes as of the adoption of JSR 310.
For Java 6 & 7, see the bullets below for ThreeTen-Backport project, and ThreeTen-ABP for earlier Android.
Always use ISO 8601 strings
When storing or exchanging date-time values as text, use Strings in the standard ISO 8601 format. These are unambiguous, easy to read by humans across cultures, and easy to parse by machine.
Immutable objects
I would not choose Date is that it is mutable.
Using immutable objects makes sense for date-time values. The java.time classes use immutable objects as part of the strategy to be inherently thread-safe.
Presentation
to parse and display dates in a GUI
Keep separate the tracking of moments, which is usually best done using Instant
, from generating text for presentation to users. For presentation, adjust into a time zone (ZoneId
) to get a ZonedDateTime
, from which you can generate text using a DateTimeFormatter
.
I am concerned only about the storage and representation of date in the data model/API
For storage, adjust to UTC by working with Instant
class. Generate ISO 8601 strings by default using toString
method. For representation in your data model, use Instant
for a UTC value unless you have a specific need per your business rules to consider a specific time zone, in which case use ZonedDateTime
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.