Increment or decrement a field of a calendar by 1 using the roll() method in Java

Description

The following code shows how to increment or decrement a field of a calendar by 1 using the roll() method.

By +1 or -1, depending on the second argument(true or false).

Example


/*from w  w  w  . j  a va  2 s  . c o m*/
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.roll(Calendar.MONTH, false); // Go back a month
    System.out.println(calendar.get(Calendar.MONTH));
  }
}

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