Increment and Decrement Months Using the Calendar in Java

Description

The following code shows how to increment and Decrement Months Using the Calendar.

Example


//from  ww  w.  jav  a 2 s.com
import java.util.Calendar;

public class Main {

  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    System.out.println("Now : " + cal.getTime());

    int monthsToDecrement = -1;
    cal.add(Calendar.MONTH, monthsToDecrement);
    System.out.println("Date after decrement: " + cal.getTime());
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone