Create new Calendar instance

static Calendar getInstance()
Gets a calendar using the default time zone and locale.
static Calendar getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
static Calendar getInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale.
static Calendar getInstance(TimeZone zone, Locale aLocale)
Gets a calendar with the specified time zone and locale.

The following code creates Calendar instance and then adds or substracts weeks to current date


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Now: " + (now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

    // add week to current date
    now.add(Calendar.WEEK_OF_MONTH, 1);
    System.out.println("date after one week : " + (now.get(Calendar.MONTH) + 1)
        + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    
    // add minus value
    
    now = Calendar.getInstance();
    now.add(Calendar.WEEK_OF_MONTH, -1);
    System.out.println("date before 1 week : "
        + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE) + "-"
        + now.get(Calendar.YEAR));
  }
}

The output:


Now: 10-30-2010
date after one week : 11-6-2010
date before 1 week : 10-23-2010

Create a Calendar object and add or substract years to current date


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

    // add year to current date
    now.add(Calendar.YEAR, 1);
    System.out.println("1 year after: " + (now.get(Calendar.MONTH) + 1)
        + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
    
    // add minus value
    now = Calendar.getInstance();
    now.add(Calendar.YEAR, -1);
    System.out.println("1 year before: " + (now.get(Calendar.MONTH) + 1) + "-"
        + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
  }
}

The output:


Current date : 10-30-2010
1 year after: 10-30-2011
1 year before: 10-30-2009
Home 
  Java Book 
    Essential Classes  

Calendar:
  1. Calendar class
  2. Constants value in Calendar
  3. Create new Calendar instance
  4. Get field value from Calendar
  5. Compare two Calendar values
  6. Calendar and display name
  7. Is lenient
  8. Add/set value to a field and get new calendar value
  9. Convert Calendar value to string value