CalendarDemo.java

Download CalendarDemo.java

// Code to display all available Calendar field values,
// for the current time (when the program is run).
// The display name in the current local is also shown,
// using the Java 6 methods.
//
// Calendar was a fix for the Date class, but was not well done.
// Java 7 or 8 will likely add a new set of classes that work
// much more intuitaively, based on the open source "Joda Time"
// project (See JSR-310 "Date and Time API").
//
// Written 11/2008 by Wayne Pollock, Tampa Florida USA

import java.util.*;
import static java.util.Calendar.*;
import static java.lang.System.out;

class CalendarDemo {

public static void main ( String [] args ) {

Calendar cal = Calendar.getInstance();
Locale locale = Locale.getDefault();

 // print out individual Calendar fields and Display names (if any):
 out.println("ERA: " + cal.get(ERA)
     + " - " + cal.getDisplayName( ERA, LONG, locale));

 out.println("YEAR: " + cal.get(YEAR)
     + " - " + cal.getDisplayName( YEAR, LONG, locale));

 out.println("MONTH: " + cal.get(MONTH)
     + " - " + cal.getDisplayName( MONTH, LONG, locale));

 out.println("WEEK_OF_YEAR: " + cal.get(WEEK_OF_YEAR)
     + " - " + cal.getDisplayName( WEEK_OF_YEAR, LONG, locale));

 out.println("WEEK_OF_MONTH: " + cal.get(WEEK_OF_MONTH)
     + " - " + cal.getDisplayName( WEEK_OF_MONTH, LONG, locale));

 out.println("DATE: " + cal.get(DATE)  // Same as DAY_OF_MONTH
     + " - " + cal.getDisplayName( DATE, LONG, locale));

 out.println("DAY_OF_MONTH: " + cal.get(DAY_OF_MONTH)
     + " - " + cal.getDisplayName( DAY_OF_MONTH, LONG, locale));

 out.println("DAY_OF_YEAR: " + cal.get(DAY_OF_YEAR)
     + " - " + cal.getDisplayName( DAY_OF_YEAR, LONG, locale));

 out.println("DAY_OF_WEEK: " + cal.get(DAY_OF_WEEK)
     + " - " + cal.getDisplayName( DAY_OF_WEEK, LONG, locale));

 out.println("DAY_OF_WEEK_IN_MONTH: " + cal.get(DAY_OF_WEEK_IN_MONTH)
     + " - " + cal.getDisplayName( DAY_OF_WEEK_IN_MONTH, LONG, locale));

 out.println("AM_PM: " + cal.get(AM_PM)
     + " - " + cal.getDisplayName( AM_PM, LONG, locale));

 out.println("HOUR: " + cal.get(HOUR)
     + " - " + cal.getDisplayName( HOUR, LONG, locale));

 out.println("HOUR_OF_DAY: " + cal.get(HOUR_OF_DAY)
     + " - " + cal.getDisplayName( HOUR_OF_DAY, LONG, locale));

 out.println("MINUTE: " + cal.get(MINUTE)
     + " - " + cal.getDisplayName( MINUTE, LONG, locale));

 out.println("SECOND: " + cal.get(SECOND)
     + " - " + cal.getDisplayName( SECOND, LONG, locale));

 out.println("MILLISECOND: " + cal.get(MILLISECOND)
     + " - " + cal.getDisplayName( MILLISECOND, LONG, locale));

 out.println("ZONE_OFFSET: "
                    + (cal.get(ZONE_OFFSET)/(60*60*1000))
     + " - " + cal.getDisplayName( ZONE_OFFSET, LONG, locale));

 out.println("DST_OFFSET: "
                    + (cal.get(DST_OFFSET)/(60*60*1000))
     + " - " + cal.getDisplayName( DST_OFFSET, LONG, locale));
}
}