What is Period.getUnits() in Java?

getUnits() is an instance method of the Period class used to retrieve all the units supported by the Period class. The units supported by the Period class are as follows:

  • Years
  • Months
  • Days

The getUnits() method is defined in the Period class. The Period class is defined in the java.time package. To import the Period class check the following import statement.

import java.time.Period;

Syntax


public List<TemporalUnit> getUnits()

Parameters

The method has no parameters.

Return value

This method returns a list of units supported by the Period class.

Code

import java.time.Period;
public class Main{
public static void main(String[] args) {
int numMonths = 0;
int numYears = 0;
int numDays = 0;
Period period = Period.of(numYears, numMonths, numDays);
System.out.printf("The supported units of the %s is %s\n", period, period.getUnits());
}
}

Free Resources