/Users/lyon/j4p/src/net/server/dates/JspCalendar.java
|
1 /*
2 * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * This software is the confidential and proprietary information of Sun
5 * Microsystems, Inc. ("Confidential Information"). You shall not
6 * disclose such Confidential Information and shall use it only in
7 * accordance with the terms of the license agreement you entered into
8 * with Sun.
9 *
10 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
11 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
13 * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
14 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
15 * THIS SOFTWARE OR ITS DERIVATIVES.
16 *
17 */
18
19 package net.server.dates;
20
21 import java.util.Calendar;
22 import java.util.Date;
23 import java.util.TimeZone;
24
25 public class JspCalendar {
26 Calendar calendar = null;
27
28 public JspCalendar() {
29 calendar = Calendar.getInstance(TimeZone.getTimeZone("PST"));
30 Date trialTime = new Date();
31 calendar.setTime(trialTime);
32 }
33
34 public int getYear() {
35 return calendar.get(Calendar.YEAR);
36 }
37
38 public String getMonth() {
39 int m = getMonthInt();
40 String[] months = new String[]{"January", "February", "March",
41 "April", "May", "June",
42 "July", "August", "September",
43 "October", "November", "December"};
44 if (m > 12)
45 return "Unknown to Man";
46
47 return months[m - 1];
48
49 }
50
51 public String getDay() {
52 int x = getDayOfWeek();
53 String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday",
54 "Thursday", "Friday", "Saturday"};
55
56 if (x > 7)
57 return "Unknown to Man";
58
59 return days[x - 1];
60
61 }
62
63 public int getMonthInt() {
64 return 1 + calendar.get(Calendar.MONTH);
65 }
66
67 public String getDate() {
68 return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear();
69
70 }
71
72 public String getTime() {
73 return getHour() + ":" + getMinute() + ":" + getSecond();
74 }
75
76 public int getDayOfMonth() {
77 return calendar.get(Calendar.DAY_OF_MONTH);
78 }
79
80 public int getDayOfYear() {
81 return calendar.get(Calendar.DAY_OF_YEAR);
82 }
83
84 public int getWeekOfYear() {
85 return calendar.get(Calendar.WEEK_OF_YEAR);
86 }
87
88 public int getWeekOfMonth() {
89 return calendar.get(Calendar.WEEK_OF_MONTH);
90 }
91
92 public int getDayOfWeek() {
93 return calendar.get(Calendar.DAY_OF_WEEK);
94 }
95
96 public int getHour() {
97 return calendar.get(Calendar.HOUR_OF_DAY);
98 }
99
100 public int getMinute() {
101 return calendar.get(Calendar.MINUTE);
102 }
103
104
105 public int getSecond() {
106 return calendar.get(Calendar.SECOND);
107 }
108
109 public static void main(String args[]) {
110 JspCalendar db = new JspCalendar();
111 p("date: " + db.getDayOfMonth());
112 p("year: " + db.getYear());
113 p("month: " + db.getMonth());
114 p("time: " + db.getTime());
115 p("date: " + db.getDate());
116 p("Day: " + db.getDay());
117 p("DayOfYear: " + db.getDayOfYear());
118 p("WeekOfYear: " + db.getWeekOfYear());
119 p("era: " + db.getEra());
120 p("ampm: " + db.getAMPM());
121 p("DST: " + db.getDSTOffset());
122 p("ZONE Offset: " + db.getZoneOffset());
123 // p("TIMEZONE: " + db.getUSTimeZone());
124 }
125
126 private static void p(String x) {
127 System.out.println(x);
128 }
129
130
131 public int getEra() {
132 return calendar.get(Calendar.ERA);
133 }
134
135 public String getUSTimeZone() {
136 String[] zones = new String[]{"Hawaii", "Alaskan", "Pacific",
137 "Mountain", "Central", "Eastern"};
138
139 int index = 10 + getZoneOffset();
140
141 if (index <= 5) {
142 return zones[10 + getZoneOffset()];
143 } else
144 return "Only US Time Zones supported";
145 }
146
147 public int getZoneOffset() {
148 return calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000);
149 }
150
151
152 public int getDSTOffset() {
153 return calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000);
154 }
155
156
157 public int getAMPM() {
158 return calendar.get(Calendar.AM_PM);
159 }
160 }
161
162