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", "March",
037                                            "April", "May", "June",
038                                            "July", "August", "September",
039                                            "October", "November", "December" };
040            if (m > 12)
041                return "Unknown to Man";
042            
043            return months[m - 1];
044    
045        }
046    
047        public String getDay() {
048            int x = getDayOfWeek();
049            String[] days = new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", 
050                                          "Thursday", "Friday", "Saturday"};
051    
052            if (x > 7)
053                return "Unknown to Man";
054    
055            return days[x - 1];
056    
057        }
058        
059        public int getMonthInt() {
060            return 1 + calendar.get(Calendar.MONTH);
061        }
062    
063        public String getDate() {
064            return getMonthInt() + "/" + getDayOfMonth() + "/" +  getYear();
065    
066        }
067    
068        public String getTime() {
069            return getHour() + ":" + getMinute() + ":" + getSecond();
070        }
071    
072        public int getDayOfMonth() {
073            return calendar.get(Calendar.DAY_OF_MONTH);
074        }
075    
076        public int getDayOfYear() {
077            return calendar.get(Calendar.DAY_OF_YEAR);
078        }
079    
080        public int getWeekOfYear() {
081            return calendar.get(Calendar.WEEK_OF_YEAR);
082        }
083    
084        public int getWeekOfMonth() {
085            return calendar.get(Calendar.WEEK_OF_MONTH);
086        }
087    
088        public int getDayOfWeek() {
089            return calendar.get(Calendar.DAY_OF_WEEK);
090        }
091         
092        public int getHour() {
093            return calendar.get(Calendar.HOUR_OF_DAY);
094        }
095        
096        public int getMinute() {
097            return calendar.get(Calendar.MINUTE);
098        }
099    
100    
101        public int getSecond() {
102            return calendar.get(Calendar.SECOND);
103        }
104    
105        public static void main(String args[]) {
106            JspCalendar db = new JspCalendar();
107            p("date: " + db.getDayOfMonth());
108            p("year: " + db.getYear());
109            p("month: " + db.getMonth());
110            p("time: " + db.getTime());
111            p("date: " + db.getDate());
112            p("Day: " + db.getDay());
113            p("DayOfYear: " + db.getDayOfYear());
114            p("WeekOfYear: " + db.getWeekOfYear());
115            p("era: " + db.getEra());
116            p("ampm: " + db.getAMPM());
117            p("DST: " + db.getDSTOffset());
118            p("ZONE Offset: " + db.getZoneOffset());
119            p("TIMEZONE: " + db.getUSTimeZone());
120        }
121    
122        private static void p(String x) {
123            System.out.println(x);
124        }
125    
126    
127        public int getEra() {
128            return calendar.get(Calendar.ERA);
129        }
130    
131        public String getUSTimeZone() {
132            String[] zones = new String[] {"Hawaii", "Alaskan", "Pacific",
133                                           "Mountain", "Central", "Eastern"};
134            
135            return zones[10 + getZoneOffset()];
136        }
137    
138        public int getZoneOffset() {
139            return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000);
140        }
141    
142    
143        public int getDSTOffset() {
144            return calendar.get(Calendar.DST_OFFSET)/(60*60*1000);
145        }
146    
147        
148        public int getAMPM() {
149            return calendar.get(Calendar.AM_PM);
150        }
151    }
152