View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  package dates;
18  
19  import java.util.Calendar;
20  import java.util.Date;
21  
22  public class JspCalendar {
23      Calendar calendar = null;
24  
25      public JspCalendar() {
26          calendar = Calendar.getInstance();
27          Date trialTime = new Date();
28          calendar.setTime(trialTime);
29      }
30  
31      public int getYear() {
32          return calendar.get(Calendar.YEAR);
33      }
34  
35      public String getMonth() {
36          int m = getMonthInt();
37          String[] months = new String[]{"January", "February", "March",
38                  "April", "May", "June",
39                  "July", "August", "September",
40                  "October", "November", "December"};
41          if (m > 12)
42              return "Unknown to Man";
43  
44          return months[m - 1];
45  
46      }
47  
48      public String getDay() {
49          int x = getDayOfWeek();
50          String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday",
51                  "Thursday", "Friday", "Saturday"};
52  
53          if (x > 7)
54              return "Unknown to Man";
55  
56          return days[x - 1];
57  
58      }
59  
60      public int getMonthInt() {
61          return 1 + calendar.get(Calendar.MONTH);
62      }
63  
64      public String getDate() {
65          return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear();
66  
67      }
68  
69      public String getTime() {
70          return getHour() + ":" + getMinute() + ":" + getSecond();
71      }
72  
73      public int getDayOfMonth() {
74          return calendar.get(Calendar.DAY_OF_MONTH);
75      }
76  
77      public int getDayOfYear() {
78          return calendar.get(Calendar.DAY_OF_YEAR);
79      }
80  
81      public int getWeekOfYear() {
82          return calendar.get(Calendar.WEEK_OF_YEAR);
83      }
84  
85      public int getWeekOfMonth() {
86          return calendar.get(Calendar.WEEK_OF_MONTH);
87      }
88  
89      public int getDayOfWeek() {
90          return calendar.get(Calendar.DAY_OF_WEEK);
91      }
92  
93      public int getHour() {
94          return calendar.get(Calendar.HOUR_OF_DAY);
95      }
96  
97      public int getMinute() {
98          return calendar.get(Calendar.MINUTE);
99      }
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