unix time - calculating current hour in Java unix time - calculating current hour in Java unix unix

unix time - calculating current hour in Java


SimpleDateFormat df = new SimpleDateFormat("hh:mm");String time = df.format(new Date(System.currentTimeMillis()));


tl;dr

ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )             .getHour();

java.time

Question and other Answers use troublesome old legacy date-time classes now supplanted by java.time.

Get current moment in UTC.

Instant instant = Instant.now();

Apply a time zone. For any given moment the date and the time-of-day vary around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );ZonedDateTime zdt = instant.at( z );

Interrogate for hour-of-day.

int hourOfDay = zdt.getHour();

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.

Count-from-epoch

In comments you mention wanting to track time as a count of milliseconds from epoch.

I strongly recommend against this for multiple reasons. It makes debugging very difficult because a human cannot read a long integer and decipher a date-time meaning. So bugs may go undetected. A count-from-epoch is not self-documenting and the recipient may be confused as to its meaning. Some people count whole seconds from epoch, others milliseconds, others microseconds, others nanoseconds, and still others use other granularities. And as for epoch, which one? There are a couple dozen different points in time used as an epoch reference in various computing systems.

But if you insist on using a count of milliseconds since the epoch of start of 1970 in UTC ( 1970-01-01T00:00:00 ), generate this number from an Instant.

long millisecondsSinceEpochOf1970 = Instant.now().toEpochMilli() ;

Instead, I suggest… When exchanging date-time values as strings, use standard ISO 8601 formats. These formats are human-readable, intuitive even across various cultures, self-documenting, recognizable, easy-to-parse, and sensible.

Generally you should be working in UTC. The ISO 8601 format for UTC values canonically uses a Z at the end, short for Zulu, and means UTC.

2016-09-16T20:46:01Z

This format is used by default when parsing/generating strings in the Instant class.

Instant.parse( "2016-09-16T20:46:01Z" )myInstant.toString()  // Result: 2016-09-16T20:46:01Z


Use Calendar

int hourOfDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);