Given an epoch time: eg (1513213212) I should get 1 since its 1 am right now UTC. How would I go about converting it into the hour of the day? Is it possible to do it just using math (division, mod)?
-
1You could use Math, sure, but LocalTime class exists for a reason – OneCricketeer Dec 14 '17 at 01:03
-
5*"Is it possible to do it just using math (division, mod)?"* - Strictly speaking, yes, but you shouldn't, as date/time mathematics are complicated and have to cover a large number of edge cases (there aren't always 24 hours in day or 365 days in a year). You're better off using the Date/Time API available in Java 8+ or if you're really stuck, `Calendar` or `java.util.Date` – MadProgrammer Dec 14 '17 at 01:04
-
Take a look at this: https://stackoverflow.com/questions/30709897/convert-millisecond-to-joda-date-time-or-for-zone-0000 – Prashant Saraswat Dec 14 '17 at 01:11
-
1There are already many questions about conversion from Unix time. For your own sake it’s better to search before asking. If what you find is insufficient, explain how, and we can guide you much better. See for example [thie question: Java: Date from unix timestamp](https://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp). – Ole V.V. Dec 14 '17 at 06:09
2 Answers
It would be close to impossible to do it by using maths only. (Leap year and all). It's better to use established APIs which will do all the hard work.
You can use following method to do this.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(1513213212* 1000L);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(cal.get(Calendar.HOUR));//12 hour clock
System.out.println(cal.get(Calendar.HOUR_OF_DAY));//24 hour clock

- 10,333
- 4
- 33
- 71
-
3Please don’t teach the young ones to use the long outdated `Calendar` class. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 14 '17 at 06:03
-
Did we interpret the question differently when “the hour of the day” was asked for? If the second value was 1513260000, I understood 14 as the correct answer. Your code prints 2. – Ole V.V. Dec 14 '17 at 06:59
-
1I'm not sure if OP is young or not, it only proves I'm old :). You are right, I was using the 12 hour format. Added 24 hour format as well. – 11thdimension Dec 14 '17 at 14:51
Use java.time
, the modern Java date and time API also known as JSR-310:
LocalTime timeOfDay = Instant.ofEpochSecond(1513213212L)
.atOffset(ZoneOffset.UTC)
.toLocalTime();
System.out.println(timeOfDay);
int hourOfDay = timeOfDay.getHour();
System.out.println(hourOfDay);
This prints:
01:00:12
1
Even if you just wanted to do the math, I would still prefer to use standard library methods for it:
long epochSeconds = 1513213212L;
// convert the seconds to days and back to seconds to get the seconds in a whole number of days
long secondsInWholeDays = TimeUnit.DAYS.toSeconds(TimeUnit.SECONDS.toDays(epochSeconds));
long hourOfDay = TimeUnit.SECONDS.toHours(epochSeconds - secondsInWholeDays);
System.out.println(hourOfDay);
This too prints 1
.
Your intention was “Given an epoch time: eg (1513213212) I should get 1 since it’s 1 AM right now UTC.” Which of the above code snippets in your opinion most clearly expresses this intention? This is what I would use for making my pick.
While MadProgrammer is surely correct in his/her comment that date and time arithmetic is complicated and that you should therefore leave it to the date and time API, I believe that this is one of the rare cases where not too complicated math gives the correct answer. It depends on it being safe to ignore the issue of leap seconds, and if going for the math solution, you should make sure to check this assumption. Personally I would not use it anyway.

- 81,772
- 15
- 137
- 161