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