tl;dr
Instant.EPOCH
Using java.time
The troublesome old date-time classes including Date
and Calendar
are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Android (see below).
To get the date-only value of the Java & Unix epoch reference date of 1970-01-01
, use LocalDate
. The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate epoch = LocalDate.ofEpochDay( 0L ) ;
epoch.toString: 1970-01-01
To get the date-time value of that same epoch, use the constant Instant.EPOCH
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant epoch = Instant.EPOCH ;
epoch.toString(): 1970-01-01T00:00:00Z
The Z
in that standard ISO 8601 output is short for Zulu
and means UTC.
To get a number of years, months, days since then, use the Period
class.
Period period = Period.between(
LocalDate.ofEpochDay( 0 ) ,
LocalDate.now( ZoneId.of( "America/Montreal" ) )
) ;
Search Stack Overflow for more discussion and examples of Period
.
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.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android