Java Time Readable Format humanTime(long seconds)

Here you can find the source of humanTime(long seconds)

Description

human Time

License

Open Source License

Declaration

public static String humanTime(long seconds) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String humanTime(long seconds) {
        long diff[] = new long[] { 0, 0, 0, 0 };
        /* sec */diff[3] = (seconds >= 60 ? seconds % 60 : seconds);
        /* min */diff[2] = (seconds = (seconds / 60)) >= 60 ? seconds % 60 : seconds;
        /* hours */diff[1] = (seconds = (seconds / 60)) >= 24 ? seconds % 24 : seconds;
        /* days */diff[0] = (seconds = (seconds / 24));

        /*/*from  w ww .  j a  va  2 s.c o  m*/
        String text = "";
        if (diff[0] > 1)
           text += "%d d%s";
        if (diff[1] > 1)
           text += "%d h%s";
        if (diff[1] > 1)
           text += "%d m%s";
        if (diff[1] > 1)
           text += "%d s%s";
        */

        String text = String.format(
                //"%d hour%s, %d minute%s, %d second%s",
                "%d days %02d:%02d:%02d", diff[0],
                //diff[0] > 1 ? "s" : "",
                diff[1],
                //diff[1] > 1 ? "s" : "",
                diff[2],
                //diff[2] > 1 ? "s" : "",
                diff[3]
        //diff[3] > 1 ? "s" : ""
        );

        return text;
    }
}

Related

  1. getMaxTimeByStringDate(String date)
  2. humanTime(long ms)
  3. humanTime(long start, long end)
  4. toHumanable(long time_millsecod)
  5. toTime(int duration)
  6. toTime(long ms)