Java Millisecond Convert convertMillisToString(long diff)

Here you can find the source of convertMillisToString(long diff)

Description

convert Millis To String

License

Apache License

Declaration

static public String convertMillisToString(long diff) 

Method Source Code

//package com.java2s;
/**// ww w  .  j  a v a 2  s. c  om
 *Copyright [2010] [David Hardtke]
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

public class Main {
    public static long MILLIS_PER_YEAR = 1000L * 60L * 60L * 24L * 365L;
    public static long MILLIS_PER_MONTH = 1000L * 60L * 60L * 24L * 30L;
    public static long MILLIS_PER_WEEK = 1000L * 60L * 60L * 24L * 7L;
    public static long MILLIS_PER_DAY = 1000L * 60L * 60L * 24L;
    public static long MILLIS_PER_HOUR = 1000L * 60L * 60L;
    public static long MILLIS_PER_MINUTE = 1000L * 60L;
    public static long MILLIS_PER_SECOND = 1000L;

    static public String convertMillisToString(long diff) {
        if (diff == 0)
            return "";
        try {
            long number = 0;
            String unit = "";
            if (diff / MILLIS_PER_YEAR > 0) {
                number = diff / MILLIS_PER_YEAR;
                unit = "year";
            } else if (diff / MILLIS_PER_MONTH > 0) {
                number = diff / MILLIS_PER_MONTH;
                unit = "month";
            } else if (diff / MILLIS_PER_WEEK > 0) {
                number = diff / MILLIS_PER_WEEK;
                unit = "week";
            } else if (diff / MILLIS_PER_DAY > 0) {
                number = diff / MILLIS_PER_DAY;
                unit = "day";
            } else if (diff / MILLIS_PER_HOUR > 0) {
                number = diff / MILLIS_PER_HOUR;
                unit = "hour";
            } else if (diff / MILLIS_PER_MINUTE > 0) {
                number = diff / MILLIS_PER_MINUTE;
                unit = "minute";
            } else if (diff / MILLIS_PER_SECOND > 0) {
                number = diff / MILLIS_PER_SECOND;
                unit = "second";
            } else {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            sb.append(String.valueOf(number));
            sb.append(" " + unit);
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}

Related

  1. convertMillisToHumanReadableForm(long milliseconds)
  2. convertMillisToTicks(long milliseconds)
  3. convertMillisToTime(String val)
  4. convertNanosecondToMillisecondString(final long nanos)
  5. convertTimeMillisToHexString(long currentDate)