CalendarDemo.java

Download CalendarDemo.java

 1: // Code to display all available Calendar field values,
 2: // for the current time (when the program is run).
 3: // The display name in the current local is also shown,
 4: // using the Java 6 methods.
 5: //
 6: // Calendar was a fix for the Date class, but was not well done.
 7: // Java 7 or 8 will likely add a new set of classes that work
 8: // much more intuitaively, based on the open source "Joda Time"
 9: // project (See JSR-310 "Date and Time API").
10: //
11: // Written 11/2008 by Wayne Pollock, Tampa Florida USA
12: 
13: import java.util.*;
14: import static java.util.Calendar.*;
15: import static java.lang.System.out;
16: 
17: class CalendarDemo {
18: 
19: public static void main ( String [] args ) {
20: 
21: Calendar cal = Calendar.getInstance();
22: Locale locale = Locale.getDefault();
23: 
24:  // print out individual Calendar fields and Display names (if any):
25:  out.println("ERA: " + cal.get(ERA)
26:      + " - " + cal.getDisplayName( ERA, LONG, locale));
27: 
28:  out.println("YEAR: " + cal.get(YEAR)
29:      + " - " + cal.getDisplayName( YEAR, LONG, locale));
30: 
31:  out.println("MONTH: " + cal.get(MONTH)
32:      + " - " + cal.getDisplayName( MONTH, LONG, locale));
33: 
34:  out.println("WEEK_OF_YEAR: " + cal.get(WEEK_OF_YEAR)
35:      + " - " + cal.getDisplayName( WEEK_OF_YEAR, LONG, locale));
36: 
37:  out.println("WEEK_OF_MONTH: " + cal.get(WEEK_OF_MONTH)
38:      + " - " + cal.getDisplayName( WEEK_OF_MONTH, LONG, locale));
39: 
40:  out.println("DATE: " + cal.get(DATE)  // Same as DAY_OF_MONTH
41:      + " - " + cal.getDisplayName( DATE, LONG, locale));
42: 
43:  out.println("DAY_OF_MONTH: " + cal.get(DAY_OF_MONTH)
44:      + " - " + cal.getDisplayName( DAY_OF_MONTH, LONG, locale));
45: 
46:  out.println("DAY_OF_YEAR: " + cal.get(DAY_OF_YEAR)
47:      + " - " + cal.getDisplayName( DAY_OF_YEAR, LONG, locale));
48: 
49:  out.println("DAY_OF_WEEK: " + cal.get(DAY_OF_WEEK)
50:      + " - " + cal.getDisplayName( DAY_OF_WEEK, LONG, locale));
51: 
52:  out.println("DAY_OF_WEEK_IN_MONTH: " + cal.get(DAY_OF_WEEK_IN_MONTH)
53:      + " - " + cal.getDisplayName( DAY_OF_WEEK_IN_MONTH, LONG, locale));
54: 
55:  out.println("AM_PM: " + cal.get(AM_PM)
56:      + " - " + cal.getDisplayName( AM_PM, LONG, locale));
57: 
58:  out.println("HOUR: " + cal.get(HOUR)
59:      + " - " + cal.getDisplayName( HOUR, LONG, locale));
60: 
61:  out.println("HOUR_OF_DAY: " + cal.get(HOUR_OF_DAY)
62:      + " - " + cal.getDisplayName( HOUR_OF_DAY, LONG, locale));
63: 
64:  out.println("MINUTE: " + cal.get(MINUTE)
65:      + " - " + cal.getDisplayName( MINUTE, LONG, locale));
66: 
67:  out.println("SECOND: " + cal.get(SECOND)
68:      + " - " + cal.getDisplayName( SECOND, LONG, locale));
69: 
70:  out.println("MILLISECOND: " + cal.get(MILLISECOND)
71:      + " - " + cal.getDisplayName( MILLISECOND, LONG, locale));
72: 
73:  out.println("ZONE_OFFSET: "
74:                     + (cal.get(ZONE_OFFSET)/(60*60*1000))
75:      + " - " + cal.getDisplayName( ZONE_OFFSET, LONG, locale));
76: 
77:  out.println("DST_OFFSET: "
78:                     + (cal.get(DST_OFFSET)/(60*60*1000))
79:      + " - " + cal.getDisplayName( DST_OFFSET, LONG, locale));
80: }
81: }