How to Subtract Months in Java


In this example, we will learn the method to subtract months in Java.

Source Code

package com.beginner.examples;

import java.util.Calendar;

public class SubtractMonths {
  public static void main(String[] args){
    Calendar c = Calendar.getInstance();
    System.out.println(c.get(Calendar.MONTH));
    //months subtract 1
    c.add(Calendar.MONTH, -1);
    System.out.println(c.get(Calendar.MONTH));
  }
}

Output:

6
5

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments