Period toTotalMonths() method in Java with Examples
The toTotalMonths() method of Period Class is used to obtain the total number of Months in the given period. It returns a long value depicting the same.
Syntax:
public long toTotalMonths()
Parameters: This method does not accepts any parameter.
Returns: This function returns the long value which is the total number of Months in the this Period.
Below is the implementation of Period.toTotalMonths() method:
Example 1:
import java.time.Period;
class GFG {
public static void main(String[] args)
{
String period = "P1Y2M21D" ;
Period p = Period.parse(period);
System.out.println( "Period: " + p);
System.out.println( "Total number of Months"
+ " in this Period: "
+ p.toTotalMonths());
}
}
|
Output:
Period: P1Y2M21D
Total number of Months in this Period: 14
Example 2:
import java.time.Period;
class GFG {
public static void main(String[] args)
{
String period = "-P1Y2M21D" ;
Period p = Period.parse(period);
System.out.println( "Period: " + p);
System.out.println( "Total number of Months"
+ " in this Period: "
+ p.toTotalMonths());
}
}
|
Output:
Period: P-1Y-2M-21D
Total number of Months in this Period: -14
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#toTotalMonths–
Last Updated :
28 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...