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