java.time.Period.toTotalMonths() Method Example



Description

The java.time.Period.toTotalMonths() method gets the total number of months in this period.

Declaration

Following is the declaration for java.time.Period.toTotalMonths() method.

public long toTotalMonths()

Return Value

the total number of months in the period, may be negative.

Example

The following example shows the usage of java.time.Period.toTotalMonths() method.

package com.tutorialspoint;

import java.time.Period;

public class PeriodDemo {
   public static void main(String[] args) {

      Period period = Period.ofYears(2);
      System.out.println(period.toTotalMonths());  
   }
}

Let us compile and run the above program, this will produce the following result −

24
Advertisements