Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: convert seconds into day, hour, minute and seconds using TimeUnit

I am using TimeStamp class to convert seconds into Day,Hours,Minutes,Seconds. I used following code

public static void calculateTime(long seconds) {         int day = (int)TimeUnit.SECONDS.toDays(seconds);                 long hours = TimeUnit.SECONDS.toHours(seconds) - TimeUnit.SECONDS.toHours(TimeUnit.SECONDS.toDays(seconds));         long minute = TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.SECONDS.toMinutes(TimeUnit.SECONDS.toHours(seconds));         long second = TimeUnit.SECONDS.toSeconds(seconds) - TimeUnit.SECONDS.toSeconds(TimeUnit.SECONDS.toMinutes(seconds));          System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);      } 

But I am not getting right result. For example when I called this method as calculateTime(3600) it gives me the result as Day 0 Hour 1 Minute 60 Seconds 3540 instead of Day 0 Hour 1 Minute 0 Seconds 0.

What is the wrong with my logic? Please help me.

like image 380
Android Learner Avatar asked Jul 06 '12 07:07

Android Learner


People also ask

How do you convert days seconds to hours minutes and seconds?

int seconds = (totalSeconds % 60); int minutes = (totalSeconds % 3600) / 60; int hours = (totalSeconds % 86400) / 3600; int days = (totalSeconds % (86400 * 30)) / 86400; First line - We get the remainder of seconds when dividing by number of seconds in a minutes.

How do you get HH MM SS from seconds?

To convert seconds to HH:MM:SS :Multiply the seconds by 1000 to get milliseconds. Pass the milliseconds to the Date() constructor. Use the toISOString() method on the Date object.


2 Answers

It should be like

 int day = (int)TimeUnit.SECONDS.toDays(seconds);          long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);  long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);  long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60); 

EDIT Explanation:

  1. Day calculation is correct, it does not require explanation.
  2. TimeUnit.SECONDS.toHours(seconds) will give you direct conversion from seconds to hours without consideration for days you have already calculated. Minus the hours for days you already got i.e, day*24. You now got remaining hours.
  3. Same for minute and second. You need to minus the already got hour and minutes respectively.
like image 141
Subir Kumar Sao Avatar answered Oct 11 '22 04:10

Subir Kumar Sao


You can do like this to only use TimeUnit:

public static void calculateTime(long seconds) {     int day = (int) TimeUnit.SECONDS.toDays(seconds);     long hours = TimeUnit.SECONDS.toHours(seconds) -                  TimeUnit.DAYS.toHours(day);     long minute = TimeUnit.SECONDS.toMinutes(seconds) -                    TimeUnit.DAYS.toMinutes(day) -                   TimeUnit.HOURS.toMinutes(hours);     long second = TimeUnit.SECONDS.toSeconds(seconds) -                   TimeUnit.DAYS.toSeconds(day) -                   TimeUnit.HOURS.toSeconds(hours) -                    TimeUnit.MINUTES.toSeconds(minute);     System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); } 

or the slightly shorter but maybe not as intuitive

public static void calculateTime(long seconds) {     int day = (int) TimeUnit.SECONDS.toDays(seconds);     long hours = TimeUnit.SECONDS.toHours(seconds) -                  TimeUnit.DAYS.toHours(day);     long minute = TimeUnit.SECONDS.toMinutes(seconds) -                   TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds));     long second = TimeUnit.SECONDS.toSeconds(seconds) -                   TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds));     System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second); } 
like image 21
Keppil Avatar answered Oct 11 '22 04:10

Keppil