Here you can find the source of formatDuration(final Duration duration, final boolean wrapInDurationFunction)
public static String formatDuration(final Duration duration, final boolean wrapInDurationFunction)
//package com.java2s; /*/*from ww w. ja va 2 s . com*/ * Copyright 2017 Red Hat, Inc. and/or its affiliates. * * 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. */ import java.time.Duration; public class Main { private static final long SECONDS_IN_A_MINUTE = 60; private static final long SECONDS_IN_AN_HOUR = 60 * SECONDS_IN_A_MINUTE; private static final long SECONDS_IN_A_DAY = 24 * SECONDS_IN_AN_HOUR; private static final long NANOSECONDS_PER_SECOND = 1000000000; public static String formatDuration(final Duration duration, final boolean wrapInDurationFunction) { if (duration.getSeconds() == 0 && duration.getNano() == 0) { if (wrapInDurationFunction) { return "duration( \"PT0S\" )"; } else { return "PT0S"; } } final long days = duration.getSeconds() / SECONDS_IN_A_DAY; final long hours = (duration.getSeconds() % SECONDS_IN_A_DAY) / SECONDS_IN_AN_HOUR; final long minutes = (duration.getSeconds() % SECONDS_IN_AN_HOUR) / SECONDS_IN_A_MINUTE; final long seconds = duration.getSeconds() % SECONDS_IN_A_MINUTE; final StringBuilder sb = new StringBuilder(); if (wrapInDurationFunction) { sb.append("duration( \""); } if (duration.isNegative()) { sb.append("-"); } sb.append("P"); if (days != 0) { appendToDurationString(sb, days, "D"); } if (hours != 0 || minutes != 0 || seconds != 0 || duration.getNano() != 0) { sb.append("T"); if (hours != 0) { appendToDurationString(sb, hours, "H"); } if (minutes != 0) { appendToDurationString(sb, minutes, "M"); } if (seconds != 0 || duration.getNano() != 0) { appendSecondsToDurationString(sb, seconds, duration.getNano()); } } if (wrapInDurationFunction) { sb.append("\" )"); } return sb.toString(); } private static void appendToDurationString(final StringBuilder sb, final long days, final String timeSegmentChar) { sb.append(Math.abs(days)); sb.append(timeSegmentChar); } private static void appendSecondsToDurationString(final StringBuilder sb, final long seconds, final long nanoseconds) { if (seconds < 0 && nanoseconds > 0) { if (seconds == -1) { sb.append("0"); } else { sb.append(Math.abs(seconds + 1)); } } else { sb.append(Math.abs(seconds)); } if (nanoseconds > 0) { final int pos = sb.length(); if (seconds < 0) { sb.append(2 * NANOSECONDS_PER_SECOND - nanoseconds); } else { sb.append(nanoseconds + NANOSECONDS_PER_SECOND); } eliminateTrailingZeros(sb); sb.setCharAt(pos, '.'); } sb.append('S'); } private static void eliminateTrailingZeros(final StringBuilder sb) { while (sb.charAt(sb.length() - 1) == '0') { // eliminates trailing zeros in the nanoseconds sb.setLength(sb.length() - 1); } } }