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:**

`// Java code to demonstrate toTotalMonths() method ` ` ` `import` `java.time.Period; ` ` ` `class` `GFG { ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// Get the String to be toTotalMonthsd ` ` ` `String period = ` `"P1Y2M21D"` `; ` ` ` ` ` `// Parse the String into Period ` ` ` `Period p = Period.parse(period); ` ` ` ` ` `System.out.println(` `"Period: "` `+ p); ` ` ` ` ` `// Get the total number of months ` ` ` `// using toTotalMonths() method ` ` ` `System.out.println(` `"Total number of Months"` ` ` `+ ` `" in this Period: "` ` ` `+ p.toTotalMonths()); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Period: P1Y2M21D Total number of Months in this Period: 14

**Example 2:**

`// Java code to demonstrate toTotalMonths() method ` ` ` `import` `java.time.Period; ` ` ` `class` `GFG { ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// Get the String to be toTotalMonthsd ` ` ` `String period = ` `"-P1Y2M21D"` `; ` ` ` ` ` `// Parse the String into Period ` ` ` `Period p = Period.parse(period); ` ` ` ` ` `System.out.println(` `"Period: "` `+ p); ` ` ` ` ` `// Get the total number of months ` ` ` `// using toTotalMonths() method ` ` ` `System.out.println(` `"Total number of Months"` ` ` `+ ` `" in this Period: "` ` ` `+ p.toTotalMonths()); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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–

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Period parse() method in Java with Examples
- Period getMonths() method in Java with Examples
- Period plusMonths() method in Java with Examples
- Period getDays() method in Java with Examples
- Period get() method in Java with Examples
- Period from() method in Java with Examples
- Period equals() method in Java with Examples
- Period between() method in Java with Examples
- Period negated() method in Java with Examples
- Period normalized() method in Java with Examples
- Period plusYears() method in Java with Examples
- Period isNegative() method in Java with Examples
- Period hashCode() method in Java with Examples
- Period getUnits() method in Java with Examples
- Period plusDays() method in Java with Examples
- Period plus() method in Java with Examples
- Period getYears() method in Java with Examples
- Period isZero() method in Java with Examples
- Period minus() method in Java with Examples
- Period minusDays() 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.