java.time.Period.withMonths() Method Example



Description

The java.time.Period.withMonths(int months) method returns a copy of this Period with the specified months.

Declaration

Following is the declaration for java.time.Period.withMonths(int months) method.

public Period withMonths(int months)

Parameters

months − the months to represent, may be negative.

Return Value

a Period based on this period with the requested months, not null.

Example

The following example shows the usage of java.time.Period.withMonths(int months) method.

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.ofMonths(2);
      System.out.println(period.toString());  
      period = period.withMonths(5);
      System.out.println(period.toString());  
   }
}

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

P2M
P5M
Advertisements