tl;dr
Instant.ofEpochSecond( 1_296_442_971L )
.atZone( ZoneId.of( "America/New_York" ) )
.getHour()
23
…for value: 2011-01-30T23:02:51-04:00[America/New_York]
32-bit vs 64-bit integers
As the other answers correctly noted, you must use a 64-bit long
or Long
to track the number of milliseconds since 1970 in UTC. Your number 1296442971
is the number of whole seconds since 1970 in UTC. When multiplied by 1,000 to get milliseconds-since-epoch, you overflow the limit of a 32-bit int
or Integer
.
java.time
Another problem is that you are using troublesome old legacy date-time classes, now supplanted by the java.time classes.
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
The Instant
class has a factory method for importing a number of whole seconds since epoch.
Instant instant = Instant.ofEpochSecond( 1_296_442_971L );
instant.toString(): 2011-01-31T03:02:51Z
ZonedDateTime
To see the wall-clock time of the east coast US, apply a ZoneId
for a time zone such as America/New_York
to get a ZonedDateTime
object.
ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( z );
zdt.toString(): 2011-01-30T23:02:51-04:00[America/New_York]
Interrogate for the hour-of-day in 24-hour numbering of 0-23 by calling getHour
.
int hourOfDay = zdt.getHour();
23
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.