Java Convert Months to Period
Tags: Java Period Java 8
In this Java core tutorial we learn how to convert a number of months to java.time.Period object in Java programming language.
How to convert months to Period in Java
In Java, we can use the Period.ofMonths(int months) static method to instantiate a new Period object from number of months as the example Java code below.
ConvertMonthsToPeriodExample1.java
import java.time.Period;
public class ConvertMonthsToPeriodExample1 {
public static void main(String... args) {
int numberOfMonths = 36;
Period period = Period.ofMonths(numberOfMonths);
System.out.println("Number of months: " + numberOfMonths);
System.out.println("Period: " + period);
}
}
Number of months: 36
Period: P36M
Happy Coding 😊