KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > utility > beans > dates > JspCalendar


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.utility.beans.dates;
17
18 import java.text.DateFormat JavaDoc;
19 import java.util.*;
20 import javax.servlet.ServletRequest JavaDoc;
21
22 /**
23  * Author Mandar Raje.
24  */

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