The Java Program: CalendarManipulation.java

  1 import java.text.*;
  2 import java.util.*;
  3 
  4 public class CalendarManipulation {
  5   public static void main(String s[]) {
  6     Calendar cal = Calendar.getInstance();
  7     DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
  8                                                    DateFormat.MEDIUM);
  9     // print out the current date and time
 10     System.out.println(df.format(cal.getTime()));
 11 
 12     // add 8 days to the current date and print out the date and time
 13     cal.add(Calendar.DATE, 8);
 14     System.out.println(df.format(cal.getTime()));
 15 
 16     // subtract 4 hours from the time and print out the date and time
 17     cal.add(Calendar.HOUR, -4);
 18     System.out.println(df.format(cal.getTime()));
 19 
 20     // add 12 hours to the current time and print out the date and time
 21     cal.add(Calendar.AM_PM, 1);
 22     System.out.println(df.format(cal.getTime()));
 23 
 24     // add another 12 hours and print out the date and time
 25     cal.add(Calendar.AM_PM, 1);
 26     System.out.println(df.format(cal.getTime()));
 27   }
 28 }
 29