Java Convert Days Months Years to Period

Tags: Java Period Java 8

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

How to convert days months and years to Period in Java

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

ConvertDaysMonthsYearsToPeriodExample1.java

import java.time.Period;

public class ConvertDaysMonthsYearsToPeriodExample1 {
    public static void main(String... args) {
        int numberOfDays = 70;
        int numberOfMonths = 5;
        int numberOfYears = 4;

        Period period = Period.of(numberOfYears, numberOfMonths, numberOfDays);

        System.out.println("Number of days: " + numberOfDays);
        System.out.println("Number of months: " + numberOfMonths);
        System.out.println("Number of years: " + numberOfYears);
        System.out.println("Period: " + period);
    }
}
The output as below.
Number of days: 70
Number of months: 5
Number of years: 4
Period: P4Y5M70D

Happy Coding 😊

Java Convert Years to Period

Java Convert Months to Period

Java Convert Weeks to Period

Java Convert Days to Period

Java Convert String to Period