KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cal > JspCalendar


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

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