Java Convert Days to Duration

Tags: Java Duration Java 8

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

How to convert days to Duration in Java

In Java, we can use the Duration.ofDays(long days) static method to instantiate a Duration object from number of days as the example Java code below.

ConvertDaysToDurationExample1.java

import java.time.Duration;

public class ConvertDaysToDurationExample1 {
    public static void main(String... args) {
        long numberOfDays = 3;

        // Convert number of days to Duration
        Duration duration = Duration.ofDays(numberOfDays);

        System.out.println("Number of days: " + numberOfDays);
        System.out.println("Duration: " + duration);
    }
}
The output as below.
Number of days: 3
Duration: PT72H

Happy Coding 😊

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 String to Duration

Java Convert Duration to Seconds