Java Duration Format format8601Duration(long duration)

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

Description

Format an ISO 8601 Duration from the number of milliseconds

License

Open Source License

Parameter

Parameter Description
duration Duration time in MS

Return

A string formatted according to ISO8601 Duration e.g. P2H1M15S

Declaration

public static String format8601Duration(long duration) 

Method Source Code

//package com.java2s;
/*/*  w ww.j  a  v a2s  .  co  m*/
 This file is part of Ustad Mobile.

 Ustad Mobile Copyright (C) 2011-2014 UstadMobile Inc.

 Ustad Mobile is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version with the following additional terms:

 All names, links, and logos of Ustad Mobile and Toughra Technologies FZ
 LLC must be kept as they are in the original distribution.  If any new
 screens are added you must include the Ustad Mobile logo as it has been
 used in the original distribution.  You may not create any new
 functionality whose purpose is to diminish or remove the Ustad Mobile
 Logo.  You must leave the Ustad Mobile logo as the logo for the
 application to be used with any launcher (e.g. the mobile app launcher).

 If you want a commercial license to remove the above restriction you must
 contact us.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 Ustad Mobile is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 */

public class Main {
    /**
     * Format an ISO 8601 Duration from the number of milliseconds
     * 
     * @param duration Duration time in MS
     * 
     * @return A string formatted according to ISO8601 Duration e.g. P2H1M15S
     */
    public static String format8601Duration(long duration) {
        int msPerHour = (1000 * 60 * 60);
        int hours = (int) Math.floor(duration / msPerHour);
        long durationRemaining = duration % msPerHour;

        int msPerMin = (60 * 1000);
        int mins = (int) Math.floor(durationRemaining / msPerMin);
        durationRemaining = durationRemaining % msPerMin;

        int msPerS = 1000;
        int secs = (int) Math.floor(durationRemaining / msPerS);

        String retVal = "PT" + hours + "H" + mins + "M" + secs + "S";
        return retVal;
    }
}

Related

  1. durationToString(long durationInMillisLong)
  2. durationToString(long durationMs)
  3. durationToString(Long ms)
  4. durationToString(long x)
  5. durationTranslator(String sonarDuration)
  6. formatBuildDuration(final long duration)
  7. formatDuration(double dblSeconds)
  8. formatDuration(Duration d)
  9. formatDuration(Duration duration)