Java Duration Format durationToString(long durationInMilliSeconds)

Here you can find the source of durationToString(long durationInMilliSeconds)

Description

duration To String

License

Apache License

Declaration

public static String durationToString(long durationInMilliSeconds) 

Method Source Code

//package com.java2s;
/**//  w ww .j  av  a2s.c  o  m
 * Copyright 2004-2009 Tobias Gierke <tobias.gierke@code-sourcery.de>
 *
 * 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 {
    private static final long SECOND = 1;
    private static final long MINUTE = 60 * SECOND;
    private static final long HOUR = 60 * MINUTE;
    private static final long DAY = 24 * HOUR;
    private static final long MONTH = 4 * 7 * DAY;

    public static String durationToString(long durationInMilliSeconds) {

        long duration = durationInMilliSeconds / 1000; // => seconds

        StringBuilder result = new StringBuilder();

        if (duration >= MONTH) {
            final long value = duration / MONTH;
            duration -= (value * MONTH);
            result.append(value).append("month(s)");
        }

        if (duration >= DAY) {
            final long value = duration / DAY;
            duration -= (value * DAY);
            if (result.length() > 0) {
                result.append(", ");
            }
            result.append(value).append("d");
        }

        if (duration >= HOUR) {
            final long value = duration / HOUR;
            duration -= (value * HOUR);
            if (result.length() > 0) {
                result.append(", ");
            }
            result.append(value).append("h");
        }

        if (duration >= MINUTE) {
            final long value = duration / MINUTE;
            duration -= (value * MINUTE);
            if (result.length() > 0) {
                result.append(", ");
            }
            result.append(value).append("m");
        }

        if (duration >= SECOND) {
            final long value = duration / SECOND;
            duration -= (value * SECOND);
            if (result.length() > 0) {
                result.append(", ");
            }
            result.append(value).append("s");
        }

        return result.toString();
    }
}

Related

  1. durationString(long duration)
  2. durationToFormattedString(final Duration duration)
  3. durationToSec(int millis)
  4. durationToString(final long msec)
  5. durationToString(int millis)
  6. durationToString(long durationInMillisLong)
  7. durationToString(long durationMs)
  8. durationToString(Long ms)
  9. durationToString(long x)