Java Duration Format durationAsString(long duration)

Here you can find the source of durationAsString(long duration)

Description

duration As String

License

Apache License

Declaration

public static String durationAsString(long duration) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Pearson Education//  w  ww.  j  a v a2s . com
 * 
 * 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 millisPerSecond = 1000;
    private static final long millisPerMinute = 60 * millisPerSecond;
    private static final long millisPerHour = 60 * millisPerMinute;
    private static final long millisPerDay = 24 * millisPerHour;
    private static final long millisPerWeek = 7 * millisPerDay;
    private static final long millisPerYear = 52 * millisPerWeek;
    private static final long millisPerMonth = millisPerYear / 12;

    public static String durationAsString(long duration) {
        StringBuilder builder = new StringBuilder();

        long years = duration / millisPerYear;
        duration -= years * millisPerYear;

        long months = duration / millisPerMonth;
        duration -= months * millisPerMonth;

        long weeks = duration / millisPerWeek;
        duration -= weeks * millisPerWeek;

        long days = duration / millisPerDay;
        duration -= days * millisPerDay;

        long hours = duration / millisPerHour;
        duration -= hours * millisPerHour;

        long minutes = duration / millisPerMinute;
        duration -= minutes * millisPerMinute;

        long seconds = duration / millisPerSecond;
        long millis = duration - seconds * millisPerSecond;

        builder.append('P');
        if (years > 0) {
            builder.append(years);
            builder.append('Y');
        }
        if (months > 0) {
            builder.append(months);
            builder.append('M');
        }
        if (weeks > 0) {
            builder.append(weeks);
            builder.append('W');
        }
        if (days > 0) {
            builder.append(days);
            builder.append('D');
        }
        if (hours > 0 || minutes > 0 || seconds > 0) {
            builder.append('T');
        }
        if (hours > 0) {
            builder.append(hours);
            builder.append('H');
        }
        if (minutes > 0) {
            builder.append(minutes);
            builder.append('M');
        }
        if (seconds > 0) {
            builder.append(seconds);
            if (millis > 0) {
                builder.append('.');
                builder.append(millis);
            }
            builder.append('S');
        }

        return builder.toString();
    }
}

Related

  1. duration(int len)
  2. durationString(long duration)
  3. durationToFormattedString(final Duration duration)
  4. durationToSec(int millis)
  5. durationToString(final long msec)