Java Time Format formatTimeSec(long time)

Here you can find the source of formatTimeSec(long time)

Description

Generates string representation of given time specified as long.

License

Open Source License

Declaration

public static String formatTimeSec(long time) 

Method Source Code

//package com.java2s;
/*//from  ww  w  . j  a  v  a2s.  c  om
 * Portions of this file Copyright 1999-2005 University of Chicago
 * Portions of this file Copyright 1999-2005 The University of Southern California.
 *
 * This file or a portion of this file is licensed under the
 * terms of the Globus Toolkit Public License, found at
 * http://www.globus.org/toolkit/download/license.html.
 * If you redistribute this file, with or without
 * modifications, you must include this notice in the file.
 */

public class Main {
    /**
     * Generates string representation of given time specified
     * as long. The format is: [days][,][h][,][min][,][sec]
     *
     */
    public static String formatTimeSec(long time) {

        StringBuffer str = new StringBuffer();

        if (time < 60) {
            str.append(time + " sec");
            return str.toString();
        }

        int days = (int) time / 86400;

        if (days != 0) {
            str.append(days + " days");
            time -= days * 86400;
        }

        int hours = (int) time / 3600;

        if (hours != 0) {
            if (str.length() != 0)
                str.append(", ");
            str.append(hours + " h");
            time -= hours * 3600;
        }

        int mins = (int) time / 60;

        if (mins != 0) {
            if (str.length() != 0)
                str.append(", ");
            str.append(mins + " min");
            time -= mins * 60;
        }

        int sec = (int) time;

        if (sec != 0) {
            if (str.length() != 0)
                str.append(", ");
            str.append(sec + " sec");
        }

        return str.toString();
    }
}

Related

  1. formatTimeOffset(long offset)
  2. formatTimePart2(long number, String unit)
  3. formatTimePeriod(long millis)
  4. formatTimePeriod(long timePeriod)
  5. formatTimePeriod(long timestamp)
  6. formatTimespan(int timespan)
  7. formatTimeSpanForScheduler(long time)
  8. formatTimestamp(String timestamp)
  9. formatTimestampEnd(String timestamp)