I am trying to get the current hour out of System.currentTimeMillis()
but i can't seem to figure out how, I tried dividing the time in millisecs by 1000 which I suppose gives me seconds but I want to get the value of current hour. I guess if I do this time(ms)/1000 and then the resultant/3600 ? is it ?
Please advice.

- 237,923
- 42
- 401
- 438

- 53
- 8
-
2why can't you create a `Calendar` object and get the `hour` from that? – Rohit Jain Mar 09 '15 at 19:28
-
CurrentTimeMillis contains a year, month, day, hour, minutes, seconds etc. So if you want to get it through division, then keep this in mind. – avk Mar 09 '15 at 19:31
-
actually i have a cassandra nodes at different location with different timezone than my local, from my local i am sending traffic with data containing current hour in a query for some operations, so i want the time in millisecs to be sure i don't key in the data with my local current time as when retrieving the data i would ask the server to send me back the data from current hour... or am i misinterpreting it ? – LeakyBucket Mar 09 '15 at 19:38
-
if i send the query with data having time in millisecs i suppose it would automatically be converted to the time at server and when retrieving the data i would ask the server again with current hour in millisecs and it would return me the data .. sometime like this i am trying to achive – LeakyBucket Mar 09 '15 at 19:41
4 Answers
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
String time = df.format(new Date(System.currentTimeMillis()));

- 2,473
- 6
- 30
- 47
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

- 1
- 1

- 303,325
- 100
- 852
- 1,154
If you are looking to go the straight mathematics route, you'll want to use division and modulus.
long x = System.currentTimeMillis();
System.out.println("Seconds " + x/1000);
x /= 1000;
System.out.println("Minutes " + x/60);
x /= 60;
System.out.println("Hours " + x/60);
x /= 60;
System.out.println("Hours (24hr) UTC - " + x%24);
System.out.println("Hours (12hr) UTC - " + x%12);
This will produce the following output (at this time, it was 7:50 pm UTC)
Seconds 1425930629
Minutes 23765510
Hours 396091
Hours (24hr) UTC - 19
Hours (12hr) UTC - 7

- 454
- 4
- 18
-
Well i guess this is what i was looking for, so i was right that first to divide with 1000 to get secs and then with 3600 to get hours, like in your example you first converted secs to minutes and then mins to hours. – LeakyBucket Mar 09 '15 at 20:01