001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package dates;
017:
018: import java.text.DateFormat;
019: import java.util.*;
020:
021: public class JspCalendar {
022: Calendar calendar = null;
023:
024: public JspCalendar() {
025: calendar = Calendar.getInstance();
026: Date trialTime = new Date();
027: calendar.setTime(trialTime);
028: }
029:
030: public int getYear() {
031: return calendar.get(Calendar.YEAR);
032: }
033:
034: public String getMonth() {
035: int m = getMonthInt();
036: String[] months = new String[] { "January", "February",
037: "March", "April", "May", "June", "July", "August",
038: "September", "October", "November", "December" };
039: if (m > 12)
040: return "Unknown to Man";
041:
042: return months[m - 1];
043:
044: }
045:
046: public String getDay() {
047: int x = getDayOfWeek();
048: String[] days = new String[] { "Sunday", "Monday", "Tuesday",
049: "Wednesday", "Thursday", "Friday", "Saturday" };
050:
051: if (x > 7)
052: return "Unknown to Man";
053:
054: return days[x - 1];
055:
056: }
057:
058: public int getMonthInt() {
059: return 1 + calendar.get(Calendar.MONTH);
060: }
061:
062: public String getDate() {
063: return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear();
064:
065: }
066:
067: public String getTime() {
068: return getHour() + ":" + getMinute() + ":" + getSecond();
069: }
070:
071: public int getDayOfMonth() {
072: return calendar.get(Calendar.DAY_OF_MONTH);
073: }
074:
075: public int getDayOfYear() {
076: return calendar.get(Calendar.DAY_OF_YEAR);
077: }
078:
079: public int getWeekOfYear() {
080: return calendar.get(Calendar.WEEK_OF_YEAR);
081: }
082:
083: public int getWeekOfMonth() {
084: return calendar.get(Calendar.WEEK_OF_MONTH);
085: }
086:
087: public int getDayOfWeek() {
088: return calendar.get(Calendar.DAY_OF_WEEK);
089: }
090:
091: public int getHour() {
092: return calendar.get(Calendar.HOUR_OF_DAY);
093: }
094:
095: public int getMinute() {
096: return calendar.get(Calendar.MINUTE);
097: }
098:
099: public int getSecond() {
100: return calendar.get(Calendar.SECOND);
101: }
102:
103: public static void main(String args[]) {
104: JspCalendar db = new JspCalendar();
105: p("date: " + db.getDayOfMonth());
106: p("year: " + db.getYear());
107: p("month: " + db.getMonth());
108: p("time: " + db.getTime());
109: p("date: " + db.getDate());
110: p("Day: " + db.getDay());
111: p("DayOfYear: " + db.getDayOfYear());
112: p("WeekOfYear: " + db.getWeekOfYear());
113: p("era: " + db.getEra());
114: p("ampm: " + db.getAMPM());
115: p("DST: " + db.getDSTOffset());
116: p("ZONE Offset: " + db.getZoneOffset());
117: p("TIMEZONE: " + db.getUSTimeZone());
118: }
119:
120: private static void p(String x) {
121: System.out.println(x);
122: }
123:
124: public int getEra() {
125: return calendar.get(Calendar.ERA);
126: }
127:
128: public String getUSTimeZone() {
129: String[] zones = new String[] { "Hawaii", "Alaskan", "Pacific",
130: "Mountain", "Central", "Eastern" };
131:
132: return zones[10 + getZoneOffset()];
133: }
134:
135: public int getZoneOffset() {
136: return calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000);
137: }
138:
139: public int getDSTOffset() {
140: return calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000);
141: }
142:
143: public int getAMPM() {
144: return calendar.get(Calendar.AM_PM);
145: }
146: }
|