Java Duration Format formattedStringToDuration(final String formattedDuration)

Here you can find the source of formattedStringToDuration(final String formattedDuration)

Description

Converts a formatted String into a Duration object.

License

Open Source License

Parameter

Parameter Description
formattedDuration String in #DURATION_FORMAT

Exception

Parameter Description
DateTimeParseException when String is in wrong format

Return

duration

Declaration

public static Duration formattedStringToDuration(final String formattedDuration) 

Method Source Code

//package com.java2s;
/**/*from   w ww  .jav  a  2  s . c  o m*/
 * 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;

import java.time.temporal.TemporalAccessor;

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 formatted String into a Duration object.
     * 
     * @param formattedDuration
     *            String in {@link #DURATION_FORMAT}
     * @return duration
     * @throws DateTimeParseException
     *             when String is in wrong format
     */
    public static Duration formattedStringToDuration(final String formattedDuration) {
        if (formattedDuration == null) {
            return null;
        }

        final TemporalAccessor ta = DateTimeFormatter.ofPattern(DURATION_FORMAT).parse(formattedDuration.trim());
        return Duration.between(LocalTime.MIDNIGHT, LocalTime.from(ta));
    }
}

Related

  1. formatISO8601Duration(int[] t)
  2. formatNanoDuration(final long nanos)
  3. formatPeriod(final Period period, final boolean wrapInDurationFunction)
  4. formatSeconds(Duration duration)
  5. formatShortDuration(long ms)
  6. formatTime(long duration)
  7. formatTime(long duration)
  8. formatTimeDuration(long timeDuration)
  9. formatTS(long duration)