Increment and Decrement a Date Using the Calendar in Java

Description

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

Example


//from   ww  w .ja v a2 s.c  o m
import java.util.Calendar;

public class Main {

  public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();
    System.out.println("Now : " + cal.getTime());

    int daysToIncrement = 5;

    cal.add(Calendar.DATE, daysToIncrement);
    System.out.println("Date after increment: " + 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