Java Duration Format durationToFormattedString(final Duration duration)

Here you can find the source of durationToFormattedString(final Duration duration)

Description

Converts a Duration into a formatted String

License

Open Source License

Parameter

Parameter Description
duration duration, which will be converted into a formatted String

Return

String in the duration format, specified at

Declaration

public static String durationToFormattedString(final Duration duration) 

Method Source Code

//package com.java2s;
/**/*w  ww  .j av  a 2s.  c om*/
 * Copyright (c) 2015 Bosch Software Innovations GmbH and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    /**
     * Format of the String expected in configuration file and in the database.
     */
    public static final String DURATION_FORMAT = "HH:mm:ss";

    /**
     * Converts a Duration into a formatted String
     * 
     * @param duration
     *            duration, which will be converted into a formatted String
     * @return String in the duration format, specified at
     *         {@link #DURATION_FORMAT}
     */
    public static String durationToFormattedString(final Duration duration) {
        if (duration == null) {
            return null;
        }

        return LocalTime.ofNanoOfDay(duration.toNanos()).format(DateTimeFormatter.ofPattern(DURATION_FORMAT));
    }
}

Related

  1. duration(int len)
  2. durationAsString(long duration)
  3. durationString(long duration)
  4. durationToSec(int millis)
  5. durationToString(final long msec)
  6. durationToString(int millis)
  7. durationToString(long durationInMilliSeconds)