Java Time Format formatTimeSpanForScheduler(long time)

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

Description

Format a timespan given in ms as human readable output (using weeks as the maximum time unit and seconds as the smallest)

License

Open Source License

Parameter

Parameter Description
time time in ms

Return

human readable time

Declaration

public static String formatTimeSpanForScheduler(long time) 

Method Source Code

//package com.java2s;
/***************************************************************
 *  This file is part of the [fleXive](R) framework.
 *
 *  Copyright (c) 1999-2014//from w ww . j  a  v  a 2 s.  c o  m
 *  UCS - unique computing solutions gmbh (http://www.ucs.at)
 *  All rights reserved
 *
 *  The [fleXive](R) project is free software; you can redistribute
 *  it and/or modify it under the terms of the GNU Lesser General Public
 *  License version 2.1 or higher as published by the Free Software Foundation.
 *
 *  The GNU Lesser General Public License can be found at
 *  http://www.gnu.org/licenses/lgpl.html.
 *  A copy is found in the textfile LGPL.txt and important notices to the
 *  license from the author are found in LICENSE.txt distributed with
 *  these libraries.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  For further information about UCS - unique computing solutions gmbh,
 *  please see the company website: http://www.ucs.at
 *
 *  For further information about [fleXive](R), please see the
 *  project website: http://www.flexive.org
 *
 *
 *  This copyright notice MUST APPEAR in all copies of the file!
 ***************************************************************/

public class Main {
    /**
     * Format a timespan given in ms as human readable output
     * (using weeks as the maximum time unit and
     * seconds as the smallest)
     *
     * @param time time in ms
     * @return human readable time
     */
    public static String formatTimeSpanForScheduler(long time) {
        if (time < 0)
            return "-";

        StringBuilder res = new StringBuilder(10);
        long s = time / 1000;
        long min = 0;
        long hr = 0;
        long d = 0;
        long w = 0;
        if (s >= 60) {
            min = s / 60;
            s = s - (min * 60);
        }
        if (min >= 60) {
            hr = min / 60;
            min = min - (hr * 60);
        }
        if (hr >= 24) {
            d = hr / 24;
            hr = hr - (d * 24);
        }
        if (d >= 7) {
            w = d / 7;
            d = d - (w * 7);
        }
        if (w > 0)
            res.append(w).append("w:");
        if (d > 0)
            res.append(d).append("d:");
        if (hr > 0)
            res.append(hr).append("hr:");
        if (min > 0) {
            res.append(min).append("min:");
        }
        if (s > 0) {
            res.append(s).append("s:");
        }
        if (res.length() == 0)
            return "0s";
        //cut off last ":"
        return res.substring(0, res.length() - 1);
    }
}

Related

  1. formatTimePeriod(long millis)
  2. formatTimePeriod(long timePeriod)
  3. formatTimePeriod(long timestamp)
  4. formatTimeSec(long time)
  5. formatTimespan(int timespan)
  6. formatTimestamp(String timestamp)
  7. formatTimestampEnd(String timestamp)
  8. formatTimestampForFilename(final long timestamp)
  9. formatTimestampForLogging(final long rawNanosTimestamp)