java.time
I suggest that you use java.time, the modern Java date and time API, for your date and time work. On Java 1.6 and 1.7 this happens through the backport of java.time to those Java version, called the ThreeTen Backport (links at the bottom).
As has been discussed in the comments, you need to decide on a time zone for interpreting 0 milliseconds after the epoch into a date and time of day. Like one such comment I am picking America/New_York time zone as an example. Insert your own.
I am using these declarations:
private static final ZoneId ZONE = ZoneId.of("America/New_York");
private static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuuu-MM-dd-HH:mm:ss", Locale.ROOT);
The rest is straightforward (when you know how):
String epochString = Instant.EPOCH.atZone(ZONE).format(FORMATTER);
System.out.println(epochString);
And output is:
1969-12-31-19:00:00
As has also been said already, at the epoch of 1970-01-01 UTC it is still December 31, 1969 on the western half of the globe including the Americas and a great part of the Pacific Ocean.
Pattern letter uuuu
? With java.time you may use yyyy
for year of era (always positive) or uuuu
for a signed year. The latter is usually recommended so that you are sure to see the difference in case you (expectedly or not) come across a year from before the common era. The question uuuu
versus yyyy
in DateTimeFormatter
formatting pattern codes in Java? linked to below covers the difference extensively.
Question: Doesn’t java.time require Java 8?
java.time just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links