1 import java.text.DateFormat;
2 import java.util.*;
3
4 public class JspCalendar {
5 Calendar calendar = null;
6
7 public JspCalendar() {
8 calendar = Calendar.getInstance();
9 Date trialTime = new Date();
10 calendar.setTime(trialTime);
11 }
12
13 public int getYear() {
14 return calendar.get(Calendar.YEAR);
15 }
16
17 public String getMonth() {
18 int m = getMonthInt();
19 String[] months = new String[] { "January", "February", "March",
20 "April", "May", "June", "July", "August", "September",
21 "October", "November", "December" };
22 if (m > 12)
23 return "Unknown to Man";
24
25 return months[m - 1];
26
27 }
28
29 public String getDay() {
30 int x = getDayOfWeek();
31 String[] days = new String[] { "Sunday", "Monday", "Tuesday",
32 "Wednesday", "Thursday", "Friday", "Saturday" };
33
34 if (x > 7)
35 return "Unknown to Man";
36
37 return days[x - 1];
38
39 }
40
41 public int getMonthInt() {
42 return 1 + calendar.get(Calendar.MONTH);
43 }
44
45 public String getDate() {
46 return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear();
47
48 }
49
50 public String getTime() {
51 return getHour() + ":" + getMinute() + ":" + getSecond();
52 }
53
54 public int getDayOfMonth() {
55 return calendar.get(Calendar.DAY_OF_MONTH);
56 }
57
58 public int getDayOfYear() {
59 return calendar.get(Calendar.DAY_OF_YEAR);
60 }
61
62 public int getWeekOfYear() {
63 return calendar.get(Calendar.WEEK_OF_YEAR);
64 }
65
66 public int getWeekOfMonth() {
67 return calendar.get(Calendar.WEEK_OF_MONTH);
68 }
69
70 public int getDayOfWeek() {
71 return calendar.get(Calendar.DAY_OF_WEEK);
72 }
73
74 public int getHour() {
75 return calendar.get(Calendar.HOUR_OF_DAY);
76 }
77
78 public int getMinute() {
79 return calendar.get(Calendar.MINUTE);
80 }
81
82 public int getSecond() {
83 return calendar.get(Calendar.SECOND);
84 }
85
86 public static void main(String args[]) {
87 JspCalendar db = new JspCalendar();
88 p("date: " + db.getDayOfMonth());
89 p("year: " + db.getYear());
90 p("month: " + db.getMonth());
91 p("time: " + db.getTime());
92 p("date: " + db.getDate());
93 p("Day: " + db.getDay());
94 p("DayOfYear: " + db.getDayOfYear());
95 p("WeekOfYear: " + db.getWeekOfYear());
96 p("era: " + db.getEra());
97 p("ampm: " + db.getAMPM());
98 p("DST: " + db.getDSTOffset());
99 p("ZONE Offset: " + db.getZoneOffset());
100 p("TIMEZONE: " + db.getUSTimeZone());
101 }
102
103 private static void p(String x) {
104 System.out.println(x);
105 }
106
107 public int getEra() {
108 return calendar.get(Calendar.ERA);
109 }
110
111 public String getUSTimeZone() {
112 String[] zones = new String[] { "Hawaii", "Alaskan", "Pacific",
113 "Mountain", "Central", "Eastern" };
114
115 return zones[10 + getZoneOffset()];
116 }
117
118 public int getZoneOffset() {
119 return calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000);
120 }
121
122 public int getDSTOffset() {
123 return calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000);
124 }
125
126 public int getAMPM() {
127 return calendar.get(Calendar.AM_PM);
128 }
129 }
130