Lightster Lightster - 10 days ago 5
Android Question

How can I get the hour of the day from milliseconds in Android

I was looking for the answer on the web but no one answers exactly what I want. Say I have a milliseconds from epoch,

System.getCurrentMillis()
. I need to know the hour of the day of said millis, just the hour, from 00 to 24, ideally not as a string, but as an int.

Is there a easy way to achieve this. Currently I'm using
SimpleDateFormat("HH")
to get the string with
dateFormat
, but I'm sure there must be a better practice.

Thanks beforehand

Answer
Calendar time = Calendar.getInstance();
time.setTimeInMillis(milliseconds);
time.get(Calendar.HOUR_OF_DAY)
Comments