Period get() method in Java with Examples
Last Updated :
27 Nov, 2018
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:
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
static void getUnit( int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.get(ChronoUnit.DAYS));
}
public static void main(String[] args)
{
int year = 8 ;
int months = 5 ;
int days = 25 ;
getUnit(year, months, days);
}
}
|
Program 2:
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
static void getUnit( int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.get(ChronoUnit.YEARS));
}
public static void main(String[] args)
{
int year = 11 ;
int months = 3 ;
int days = 21 ;
getUnit(year, months, days);
}
}
|
Share your thoughts in the comments
Please Login to comment...