Period getYears() method in Java with Examples Last Updated : 06 Feb, 2023 Comments Improve Suggest changes Like Article Like Report The getYears() method of Period in Java is used to get the amount of years of this period with which it is used. Note: 15 months is not equal to 1 years and 3 months. Syntax: public List getYears() Parameters: This method does not accepts any parameter. Returns: This method returns the amounts of years in this current period. Below programs illustrate the above method: Program 1: Java // Java code to show the function getYears() // to get the number of years in the period import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodDemo { // Function to get the number of years static void getNumberOfDays(int year, int months, int days) { Period period = Period.of(year, months, days); System.out.println(period.getYears()); } // Driver Code public static void main(String[] args) { int year = 12; int months = 3; int days = 31; getNumberOfDays(year, months, days); } } Output:12 Program 2: Java // Java code to show the function getYears() // to get the number of years in the period import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodDemo { // Function to get the number of years static void getNumberOfDays(int year, int months, int days) { Period period = Period.of(year, months, days); System.out.println(period.getYears()); } // Driver Code public static void main(String[] args) { int year = -12; int months = 3; int days = 31; getNumberOfDays(year, months, days); } } Output:-12 Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#getYears-- Comment More infoAdvertise with us Next Article Period from() method in Java with Examples B barykrg Follow Improve Article Tags : Misc Java Java-Functions Java-time package Java-Period +1 More Practice Tags : JavaMisc Similar Reads Period get() method in Java with Examples The get() method of Period class in Java is used to get the value of the requested unit(YEARS, MONTHS or DAYS) given in the argument from this Period. Syntax: public long get(TemporalUnit unit) Parameters: This method accepts a single parameter unit of type TemporalUnit which is the unit to get requ 2 min read Period getDays() method in Java with Examples The getDays() method of Period class in Java is used to get the number of days in this current period with which it is used. Syntax: public int getDays() Parameters: This method does not accepts any parameter. Return Value: This function returns the number of days in the given period. Below programs 2 min read Period getUnits() method in Java with Examples The getUnits() method of Period class in Java is used to get the set of units supported by this Period. The units that are supported are YEARS, MONTHS, DAYS in a list (in this order only). Syntax: public List getUnits() Parameters: This method does not accepts any parameter. Return Value This method 2 min read Period getMonths() method in Java with Examples The getMonths() method of Period class in Java is used to get the number of months in this current period with which it is used. Syntax: public int getMonths() Parameters: This method does not accepts any parameter. Return Value: This function returns the number of months in the given period. Note: 2 min read Period from() method in Java with Examples The from() method of Period class in Java is used to get an instance of Period from given temporal amount. This function obtains a period based on the amount given in argument. A TemporalAmount represents an amount of time, which may be date-based or time-based. Syntax: public static Period from(Tem 1 min read Period of() method in Java with Examples The of() method of Period Class is used to obtain a period from given number of Years, Months, and Days as parameters. These parameters are accepted in the form of integers. This method returns a Period with the given number of years, months, and days. Syntax: public static Period of( int numberOfYe 2 min read Like