Java Convert String to Duration

Tags: String Java Duration Java 8

In this Java core tutorial we learn how to convert a String object to java.time.Duration object in Java programming language.

How to convert String to Duration in Java

In Java, we can use the Duration.parse(CharSequence text) static method to parse a textual representation of a duration String to a Duration object as the example Java code below.

ConvertStringToDurationExample1.java

import java.time.Duration;

public class ConvertStringToDurationExample1 {
    public static void main(String... args) {
        // PT0.999888777S equals 999888777 nanoseconds
        Duration duration1 = Duration.parse("PT0.999888777S");

        // PT19.123S equals 19.123 seconds
        Duration duration2 = Duration.parse("PT19.123S");

        // PT35M equals 35 minutes
        Duration duration3 = Duration.parse("PT35M");

        // PT23H equals 23 hours
        Duration duration4 = Duration.parse("PT23H");

        // P5D equals 5 days
        Duration duration5 = Duration.parse("P5D");

        // P2DT3H4M equals 2 days, 3 hours and 4 minutes
        Duration duration6 = Duration.parse("P2DT3H4M");

        // PT-6H3M equals -6 hours and +3 minutes
        Duration duration7 = Duration.parse("PT-6H3M");

        // -PT6H3M equals -6 hours and -3 minutes
        Duration duration8 = Duration.parse("-PT6H3M");

        // -PT-6H+3M equals +6 hours and -3 minutes
        Duration duration9 = Duration.parse("-PT-6H+3M");

        System.out.println("duration1 (PT0.999888777S equals 999888777 nanoseconds): " + duration1);
        System.out.println("duration2 (PT19.123S equals 19.123 seconds): " + duration2);
        System.out.println("duration3 (PT35M equals 35 minutes): " + duration3);
        System.out.println("duration4 (PT23H equals 23 hours): " + duration4);
        System.out.println("duration5 (P5D equals 5 days): " + duration5);
        System.out.println("duration6 (P2DT3H4M equals 2 days, 3 hours and 4 minutes): " + duration6);
        System.out.println("duration7 (PT-6H3M equals -6 hours and +3 minutes): " + duration7);
        System.out.println("duration8 (-PT6H3M equals -6 hours and -3 minutes): " + duration8);
        System.out.println("duration9 (-PT-6H+3M equals +6 hours and -3 minutes): " + duration9);
    }
}
The output as below.
duration1 (PT0.999888777S equals 999888777 nanoseconds): PT0.999888777S
duration2 (PT19.123S equals 19.123 seconds): PT19.123S
duration3 (PT35M equals 35 minutes): PT35M
duration4 (PT23H equals 23 hours): PT23H
duration5 (P5D equals 5 days): PT120H
duration6 (P2DT3H4M equals 2 days, 3 hours and 4 minutes): PT51H4M
duration7 (PT-6H3M equals -6 hours and +3 minutes): PT-5H-57M
duration8 (-PT6H3M equals -6 hours and -3 minutes): PT-6H-3M
duration9 (-PT-6H+3M equals +6 hours and -3 minutes): PT5H57M

Happy Coding 😊

Java Convert Days to Duration

Java Convert Hours to Duration

Java Convert Minutes to Duration

Java Convert Seconds to Duration

Java Convert Milliseconds to Duration

Java Convert Nanoseconds to Duration

Java Convert Duration to Seconds