Java Time Format formatTimespan(int timespan)

Here you can find the source of formatTimespan(int timespan)

Description

format Timespan

License

Open Source License

Declaration

public static String formatTimespan(int timespan) 

Method Source Code

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

public class Main {
    public static String formatTimespan(int timespan) {
        String result = "";
        int years = timespan / 31536000;
        if (years > 0) {
            timespan -= years * 31536000;
            result += String.format(" %dy", years);
        }//from  w w  w  . j av a 2s  .com
        int months = timespan / 2592000;
        if (months > 0) {
            timespan -= months * 2592000;
            result += String.format(" %dm", months);
        }
        int days = timespan / 86400;
        if (days > 0) {
            timespan -= days * 86400;
            result += String.format(" %dd", days);
        }
        int hours = timespan / 3600;
        if (hours > 0) {
            timespan -= hours * 3600;
            result += String.format(" %dh", hours);
        }
        int minutes = timespan / 60;
        if (minutes > 0) {
            timespan -= minutes * 60;
            result += String.format(" %dm", minutes);
        }
        if (timespan > 0) {
            result += String.format(" %ds", timespan);
        }
        return result.trim();
    }
}

Related

  1. formatTimePart2(long number, String unit)
  2. formatTimePeriod(long millis)
  3. formatTimePeriod(long timePeriod)
  4. formatTimePeriod(long timestamp)
  5. formatTimeSec(long time)
  6. formatTimeSpanForScheduler(long time)
  7. formatTimestamp(String timestamp)
  8. formatTimestampEnd(String timestamp)
  9. formatTimestampForFilename(final long timestamp)