Java Duration Format formatISO8601Duration(int[] t)

Here you can find the source of formatISO8601Duration(int[] t)

Description

format ISO8601 duration string, for example [1,0,0,1,0,0,0] → P1YT1H

License

Open Source License

Parameter

Parameter Description
t 6 or 7-element array with [year,mon,day,hour,min,sec,nanos]

Return

the formatted ISO8601 duration

Declaration

public static String formatISO8601Duration(int[] t) 

Method Source Code

//package com.java2s;
/*/*w  w w .  j  ava  2 s .  c om*/
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

public class Main {
    /**
     * format ISO8601 duration string, for example [1,0,0,1,0,0,0] → P1YT1H
     * @param t 6 or 7-element array with [year,mon,day,hour,min,sec,nanos]
     * @return the formatted ISO8601 duration
     */
    public static String formatISO8601Duration(int[] t) {
        StringBuilder result = new StringBuilder(24);
        result.append('P');
        if (t[0] != 0)
            result.append(t[0]).append('Y');
        if (t[1] != 0)
            result.append(t[1]).append('M');
        if (t[2] != 0)
            result.append(t[2]).append('D');
        if (t[3] != 0 || t[4] != 0 || t[5] != 0 || (t.length == 7 && t[6] != 0)) {
            result.append('T');
            if (t[3] != 0)
                result.append(t[3]).append('H');
            if (t[4] != 0)
                result.append(t[4]).append('M');
            if (t[5] != 0 || (t.length == 7 && t[6] != 0)) {
                if (t.length < 7 || t[6] == 0) {
                    result.append(t[5]).append('S');
                } else {
                    double sec = t[5] + t[6] / 1000000000.;
                    result.append(sec).append('S');
                }
            }
        }
        return result.toString();
    }
}

Related

  1. formatDurationHMSms(long ms)
  2. formatDurationLpad(final String s)
  3. formatDurationMills(long millis)
  4. formatDurationOneUnit(long milis)
  5. formatHuman(Duration duration)
  6. formatNanoDuration(final long nanos)
  7. formatPeriod(final Period period, final boolean wrapInDurationFunction)
  8. formatSeconds(Duration duration)
  9. formatShortDuration(long ms)