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