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

后端 未结 8 1766
我寻月下人不归
我寻月下人不归 2020-12-08 06:25

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

public static void calculateTime(long seconds) {
                 


        
相关标签:
8条回答
  • 2020-12-08 06:53

    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.
    0 讨论(0)
  • 2020-12-08 06:55

    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);
    }
    
    0 讨论(0)
  • 2020-12-08 06:56

    Try this

    public static void calculateTime(long seconds) {
    
    int day = (int)TimeUnit.SECONDS.toDays(seconds);        
    long hours = TimeUnit.SECONDS.toHours(seconds) ;
    
    long tempSec = seconds - (TimeUnit.HOURS.toSeconds(hours) );
    System.out.println("after hours calculation "+ tempSec);
    
    long minute = TimeUnit.SECONDS.toMinutes(tempSec); 
    
    if(tempSec > TimeUnit.MINUTES.toSeconds(minute)){
        tempSec = tempSec - (TimeUnit.MINUTES.toSeconds(minute) );          
    }else{
        tempSec = TimeUnit.MINUTES.toSeconds(minute) - tempSec;
    }
    System.out.println("after min calculation "+ tempSec);
    
    
    long second = TimeUnit.SECONDS.toSeconds(tempSec) ;
    
    System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
    }
    
    0 讨论(0)
  • 2020-12-08 06:58

    This is my code:

    public static String secondsToString(TimeUnit greatestUnit, long sourceDuration, TimeUnit sourceUnit) {
    
        int ordinal = greatestUnit.ordinal();
        if(ordinal<=sourceUnit.ordinal())
            return String.format("%02d", sourceDuration);
    
        final long greatestDuration = greatestUnit.convert(sourceDuration, sourceUnit);
        final long rest = sourceDuration - sourceUnit.convert(greatestDuration, greatestUnit);
    
        return String.format("%02d:", greatestDuration) + secondsToString(TimeUnit.values()[--ordinal], rest, sourceUnit);
    }
    

    or by loop

    public static String secondsToStringByLoop(TimeUnit greatestUnit, long sourceDuration, TimeUnit sourceUnit) {
    
        final StringBuffer sb = new StringBuffer();
        int ordinal = greatestUnit.ordinal();
    
        while(true){
            if(ordinal<=sourceUnit.ordinal()) {
                sb.append(String.format("%02d", sourceDuration));
                break;
            }
    
            final long greatestDuration = greatestUnit.convert(sourceDuration, sourceUnit);
    //          if(greatestDuration>0 || sb.length()>0)
            sb.append(String.format("%02d:", greatestDuration));
    
            sourceDuration -= sourceUnit.convert(greatestDuration, greatestUnit);
            greatestUnit = TimeUnit.values()[--ordinal];
        };
    
        return sb.toString();
    }
    

    usage example:

    String str = secondsToString(TimeUnit.DAYS, 1000, TimeUnit.SECONDS);
    

    function returns: "00:00:16:40" (days:hours:minutes:seconds)

    str = UnitsConverter.secondsToString(TimeUnit.DAYS, 1000, TimeUnit.MINUTES);
    

    returns: "00:16:40" (days:hours:minutes)

    str = UnitsConverter.secondsToString(TimeUnit.MINUTES, 1000, TimeUnit.SECONDS);
    

    returns: "16:40" (minutes:seconds)

    0 讨论(0)
  • 2020-12-08 06:58
    public static void timeCalculator(){
    
    
          Scanner input = new Scanner(System.in);
          System.out.print("Enter length of time in seconds: ");
          int n = input.nextInt();
          int nDay = n/86400;
          int nHours = (n%86400)/3600;
          int nMin = ((n%86400)%3600) /60;
          int nSec =(((n%86400)%3600)%60);
          System.out.println();
          System.out.print("That is "+ nDay+ " day(s),"+nHours+"  hour(s), "+nMin+"  minute(s), and "+nSec+" second(s). ");
    }
    
    0 讨论(0)
  • 2020-12-08 06:59

    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)));
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题