The example aims to add or subtract month(s) in current date and time values by java Calendar.
Source Code
package com.beginner.examples;
import java.util.Calendar;
public class ASMonthsExample {
public static void main(String[] args){
//get calendar
Calendar now = Calendar.getInstance();
System.out.println(now.get(Calendar.MONTH));
//months add 1
now.add(Calendar.MONTH, 1);
System.out.println(now.get(Calendar.MONTH));
//months subtract 1
now.add(Calendar.MONTH, -1);
System.out.println(now.get(Calendar.MONTH));
}
}
Output:
4
5
4
References
Imported packages in Java documentation: