FileDocCategorySizeDatePackage
CalendarManipulation.javaAPI DocExample1031Fri Mar 30 00:18:46 BST 2001None

CalendarManipulation.java

import java.text.*;
import java.util.*;

public class CalendarManipulation {
  public static void main(String s[]) {
    Calendar cal = Calendar.getInstance();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
                                                   DateFormat.MEDIUM);
    // print out the current date and time
    System.out.println(df.format(cal.getTime()));

    // add 8 days to the current date and print out the date and time
    cal.add(Calendar.DATE, 8);
    System.out.println(df.format(cal.getTime()));

    // subtract 4 hours from the time and print out the date and time
    cal.add(Calendar.HOUR, -4);
    System.out.println(df.format(cal.getTime()));

    // add 12 hours to the current time and print out the date and time
    cal.add(Calendar.AM_PM, 1);
    System.out.println(df.format(cal.getTime()));

    // add another 12 hours and print out the date and time
    cal.add(Calendar.AM_PM, 1);
    System.out.println(df.format(cal.getTime()));
  }
}