Java Convert Days to Period

Tags: Java Period Java 8

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

How to convert days to Period in Java

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

ConvertDaysToPeriodExample1.java

import java.time.Period;

public class ConvertDaysToPeriodExample1 {
    public static void main(String... args) {
        int numberOfDays = 999;

        Period period = Period.ofDays(numberOfDays);

        System.out.println("Number of days: " + numberOfDays);
        System.out.println("Period: " + period);
    }
}
The output as below.
Number of days: 999
Period: P999D

Happy Coding 😊

Java Convert Years to Period

Java Convert Months to Period

Java Convert Weeks to Period

Java Convert Days Months Years to Period

Java Convert String to Period