Calendar: add(int field, int amount) : Calendar « java.util « Java by API





Calendar: add(int field, int amount)

   

/*
It is now Thu 2006/05/11 at 02:11:22 PM PDT
Two years ago was Tue 2004/05/11 at 02:11:22 PM PDT
 * */

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainClass {

  public static void main(String[] a) throws Exception {

    Calendar now = Calendar.getInstance();

    SimpleDateFormat formatter = new SimpleDateFormat("E yyyy/MM/dd 'at' hh:mm:ss a zzz");
    System.out.println("It is now " + formatter.format(now.getTime()));

    now.add(Calendar.YEAR, -2);
    System.out.println("Two years ago was " + formatter.format(now.getTime()));
  }
}

           
         
    
    
  










Related examples in the same category

1.Calendar.AM_PM
2.Calendar.DATE
3.Calendar.DAY_OF_WEEK_IN_MONTH
4.Calendar: DAY_OF_WEEK
5.Calendar.DAY_OF_YEAR
6.Calendar.DST_OFFSET
7.Calendar.ERA
8.Calendar.HOUR
9.Calendar.HOUR_OF_DAY
10.Calendar.MILLISECOND
11.Calendar.MINUTE
12.Calendar.MONDAY
13.Calendar: TUESDAY
14.Calendar.SECOND
15.Calendar.SUNDAY
16.Calendar.WEEK_OF_MONTH
17.Calendar.WEEK_OF_YEAR
18.Calendar.YEAR
19.Calendar.ZONE_OFFSET
20.Calendar: after(Object when)
21.Calendar: before(Object when)
22.Calendar: int get(int field)
23.Calendar: getActualMaximum(int field)
24.Calendar: getDisplayNames(int field, int style, Locale locale)
25.Calendar: getFirstDayOfWeek()
26.Calendar: getInstance()
27.Calendar: getTime()
28.Calendar: set(int field, int value)
29.Calendar: set(int year, int month, int date)
30.Calendar: setTime(Date date)
31.Calendar: setTimeInMillis(long millis)