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 required unit.
Return Value: This function returns the long value of the requested unit.
Exceptions:
- DateTimeException – This method throws DateTimeException, if the unit in argument is not supported.
- UnsupportedTemporalTypeException – This method throws UnsupportedTemporalTypeException if the unit given in argument is not supported.
Below programs illustrate the above method:
Program 1:
// Java code to show the function get() // which gives the requested unit import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodDemo { // Function to get requested unit static void getUnit(int year, int months, int days) { Period period = Period.of(year, months, days); System.out.println(period.get(ChronoUnit.DAYS)); } // Driver Code public static void main(String[] args) { int year = 8; int months = 5; int days = 25; getUnit(year, months, days); } } |
25
Program 2:
// Java code to show the function get() // which gives the requested unit import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodDemo { // Function to get requested unit static void getUnit(int year, int months, int days) { Period period = Period.of(year, months, days); System.out.println(period.get(ChronoUnit.YEARS)); } // Driver Code public static void main(String[] args) { int year = 11; int months = 3; int days = 21; getUnit(year, months, days); } } |
11
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Period from() method in Java with Examples
- Period plus() method in Java with Examples
- Period of() method in Java with Examples
- Period between() method in Java with Examples
- Period withMonths() method in Java with Examples
- Period toTotalMonths() method in Java with Examples
- Period plusDays() method in Java with Examples
- Period plusMonths() method in Java with Examples
- Period hashCode() method in Java with Examples
- Period withDays() method in Java with Examples
- Period parse() method in Java with Examples
- Period withYears() method in Java with Examples
- Period getYears() method in Java with Examples
- Period ofWeeks() method in Java with Examples
- Period multipliedBy() method in Java with Examples
- Period minusYears() method in Java with Examples
- Period minusMonths() method in Java with Examples
- Period ofDays() method in Java with Examples
- Period isZero() method in Java with Examples
- Period minus() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

