Here you can find the source of formattedStringToDuration(final String formattedDuration)
Parameter | Description |
---|---|
formattedDuration | String in #DURATION_FORMAT |
Parameter | Description |
---|---|
DateTimeParseException | when String is in wrong format |
public static Duration formattedStringToDuration(final String formattedDuration)
//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)); } }