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

78,598

Solution 1

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.

Solution 2

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);
}

Solution 3

Simple method:

public static void calculateTime(long seconds) {
    long sec = seconds % 60;
    long minutes = seconds % 3600 / 60;
    long hours = seconds % 86400 / 3600;
    long days = seconds / 86400;

    System.out.println("Day " + days + " Hour " + hours + " Minute " + minutes + " Seconds " + sec);
}

Solution 4

Here is a code i created : (For 3600 seconds it shows "Days:0 Hours:1 Minutes:0 Seconds:0")

public class TimeConvert 
{
    public static void main(String[] args) 
    {
        int fsec,d,h,m,s,temp=0,i;
        fsec=3600;
        //For Days
        if(fsec>=86400)
        {
            temp=fsec/86400;
            d=temp;
            for(i=1;i<=temp;i++)
            {
                fsec-=86400;
            }
        }
        else
        {
            d=0;
        }
        //For Hours
        if(fsec>=3600)
        {
            temp=fsec/3600;
            h=temp;
            for(i=1;i<=temp;i++)
            {
                fsec-=3600;
            }            
        }
        else
        {
            h=0;
        }
        //For Minutes
        if(fsec>=60)
        {
            temp=fsec/60;
            m=temp;
            for(i=1;i<=temp;i++)
            {
                fsec-=60;
            }            
        }
        else
        {
            m=0;
        }
        //For Seconds
        if(fsec>=1)
        {
            s=fsec;            
        }
        else
        {
            s=0;
        }
        System.out.println("Days:"+d+" Hours:"+h+" Minutes:"+m+" Seconds:"+s);
    }
}

Hope it answers your question.

Solution 5

Late but helpful

get time in the format 00:00:00

/**
     * The time in format.
     *
     * in The Format of 00:00:00
     */

public String getTimeInFormat(long _SECONDS)
{
    if(TimeUnit.SECONDS.toHours(_SECONDS)>0)
    {
        return  String.format("%02d:%02d:%02d",
                TimeUnit.SECONDS.toHours(_SECONDS),
                TimeUnit.SECONDS.toMinutes(_SECONDS) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(_SECONDS)),
                TimeUnit.SECONDS.toSeconds(_SECONDS) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(_SECONDS)));
    }
    else {
        return  String.format("%02d:%02d",
                TimeUnit.SECONDS.toMinutes(_SECONDS) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(_SECONDS)),
                TimeUnit.SECONDS.toSeconds(_SECONDS) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(_SECONDS)));
    }

}
Share:
78,598
Android Learner
Author by

Android Learner

http://stackoverflow.com/users/edit/1109344

Updated on July 23, 2022

Comments

  • Android Learner
    Android Learner almost 2 years

    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.

  • Android Learner
    Android Learner almost 12 years
    Nice attempt. But I am using TimeUnit class, and not getting desired answer.
  • Android Learner
    Android Learner almost 12 years
    +1 Thank you for this nice answer. Sorry I have a singleoption to select right answer.
  • Keppil
    Keppil almost 12 years
    You're welcome! Adding +1 to the useful answers and selecting the one you like the best as the correct one seems like the perfect behaviour.
  • Kaveesh Kanwal
    Kaveesh Kanwal over 7 years
    so basically you have timestamp in variable second?
  • Maveňツ
    Maveňツ about 7 years
    what about months & years?
  • Martin Erlic
    Martin Erlic almost 7 years
    This is a great answer actually. A great use-case is when calculating time remaining between the present and some future target date. Just create a new method using the code above. Calculate long duration = endDate.getTime() - startDate.getTime(); and long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);. Then pass diffInSeconds as a parameter to this new method. Remove fsec from the list of integer variables. Then delete the line fsec=3600;. Either replace all instances of fsec with diffInSeconds or name the input parameter long fsec. Cheers.
  • GhostCat
    GhostCat over 6 years
    Code only answers are discouraged. And the question asks about using TimeUnit. Which you aren't using at all.