Java Second Convert secondsToStringWithUnits(double value)

Here you can find the source of secondsToStringWithUnits(double value)

Description

seconds To String With Units

License

Open Source License

Declaration

public static String secondsToStringWithUnits(double value) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may

public class Main {
    private final static long MINUTE = 60;
    private final static long HOUR = MINUTE * 60;
    private final static long DAY = HOUR * 24;
    private final static long WEEK = DAY * 7;

    public static String secondsToStringWithUnits(double value) {
        String result = value > 0 ? "" : "0 secs";
        double secondsRemaining = value;

        if (secondsRemaining > WEEK) {
            long numUnits = (long) (secondsRemaining / WEEK);
            secondsRemaining = secondsRemaining % WEEK;

            result += numUnits;/*from  www . j a v a2 s  . c  om*/
            result += numUnits > 1 ? " wks " : " wk ";
        }

        if (secondsRemaining > DAY) {
            long numUnits = (long) (secondsRemaining / DAY);
            secondsRemaining = secondsRemaining % DAY;

            result += numUnits;
            result += numUnits > 1 ? " days " : " day ";
        }

        if (secondsRemaining > HOUR) {
            long numUnits = (long) (secondsRemaining / HOUR);
            secondsRemaining = secondsRemaining % HOUR;

            result += numUnits;
            result += numUnits > 1 ? " hrs " : " hr ";
        }

        if (secondsRemaining > MINUTE) {
            long numUnits = (long) (secondsRemaining / MINUTE);
            secondsRemaining = secondsRemaining % MINUTE;

            result += numUnits;
            result += numUnits > 1 ? " mins " : " min ";
        }

        if (secondsRemaining > 0) {
            result += (long) secondsRemaining;
            result += secondsRemaining > 1 ? " secs" : " sec";
        }

        return result;
    }
}

Related

  1. secondsToString(int seconds)
  2. secondsToString(long seconds)
  3. SecondsToString(long seconds)
  4. secondsToString(long seconds)
  5. secondsToString(long time)
  6. secondsToTicks(double seconds)
  7. secondsToTicks(double x)
  8. secondsToTicks(float seconds)
  9. secondsToTicks(int seconds)