tl;dr
Instant.ofEpochSeconds( 1_373_604_190L )
.atZone( ZoneId.of( "America/New_York" ) )
.format( DateTimeFormatter.ofPattern( "dd MMM uuuu HH:mm:ss" , Locale.CANADA ) )
Details
the number of seconds from 31 Dec 1969 19:00:00 i.e. (EST)
Do not think parochially when doing date-time work. Rather than translate values such as the epoch reference moment in and out of your own zone, just think in UTC. Consider UTC to be The One True Time, all others being variations on that theme.
So, always refer to the epoch reference moment as 1970-01-01T00:00:00Z
, the first moment of 1970 in UTC, rather than in any other time zone.
(EST)
Also, never use those 3-4 letter abbreviations as time zones. Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Those 3-4 letter abbreviation such as EST
or IST
are not true time zones, not standardized, and not even unique(!).
Using java.time
The modern way to do this work is with the java.time classes.
long secondsSinceEpoch = 1_373_604_190L ;
We convert that to a Instant
object. 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 instant = Instant.ofEpochSeconds( secondsSinceEpoch ) ;
To see this value through the lens of some wall-clock time, apply a time zone as a ZoneId
to get a ZonedDateTime
object.
I am guessing that by EST
you meant the time zone used by much of the eastern portions of the United States and Canada. I will arbitrarily choose the time zone America/New_York
.
ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( "America/New_York" );
secondsSinceEpoch: 1373604190
instant.toString(): 2013-07-12T04:43:10Z
zdt.toString(): 2013-07-12T00:43:10-04:00[America/New_York]
See live code at IdeOne.com.
Strings
To generate a string, you can either let java.time automatically localize or you can specify an explicit formatting pattern.
Locale locale = Locale.CANADA ;
DateTimeFormatter formatterLocalized = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale );
String outputLocalized = zdt.format( formatterLocalized );
12-Jul-2013 12:43:10 AM
DateTimeFormatter formatterCustom = DateTimeFormatter.ofPattern( "dd MMM uuuu HH:mm:ss" , locale );
String output = zdt.format( formatterCustom );
12 Jul 2013 00:43:10
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
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.