View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.lum.common.client.widgets;
17  
18  import java.util.Date;
19  
20  /**
21   * <dl>
22   * <dt><b>Title: </b><dd>Calendar</dd>
23   * <p>
24   * <dt><b>Description: </b><dd>java.util.Calendar replacment</dd>
25   * </dl>
26   * @author <a href="mailto:andre.freller@gmail.com">Andre Freller</a>
27   * @version $Revision: 0.1 $
28   */
29  ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// ///////// /////////
30  public abstract class Calendar
31  {
32      public static final int AM                  = 0;
33      public static final int AM_PM               = 9;
34      public static final int PM                  = 1;
35  
36      public static final int JANUARY             = 0;
37      public static final int FEBRUARY            = 1;
38      public static final int MARCH               = 2;
39      public static final int APRIL               = 3;
40      public static final int MAY                 = 4;
41      public static final int JUNE                = 5;
42      public static final int JULY                = 6;
43      public static final int AUGUST              = 7;
44      public static final int SEPTEMBER           = 8;
45      public static final int OCTOBER             = 9;
46      public static final int NOVEMBER            = 10;
47      public static final int DECEMBER            = 11;
48      public static final int UNDECIMBER          = 12;
49  
50      public static final int SUNDAY              = 1;
51      public static final int MONDAY              = 2;
52      public static final int TUESDAY             = 3;
53      public static final int WEDNESDAY           = 4;
54      public static final int THURSDAY            = 5;
55      public static final int FRIDAY              = 6;
56      public static final int SATURDAY            = 7;
57  
58  
59      public static final int ERA                 = 0;
60      public static final int YEAR                = 1;
61      public static final int MONTH               = 2;
62      public static final int WEEK_OF_YEAR        = 3;
63      public static final int WEEK_OF_MONTH       = 4;
64      public static final int DATE                = 5;
65      public static final int DAY_OF_MONTH        = DATE;
66      public static final int DAY_OF_YEAR         = 6;
67      public static final int DAY_OF_WEEK         = 7;
68      public static final int DAY_OF_WEEK_IN_MONTH = 8;
69      public static final int HOUR                = 10;
70      public static final int HOUR_OF_DAY         = 11;
71      public static final int MINUTE              = 12;
72      public static final int SECOND              = 13;
73      public static final int MILLISECOND         = 14;
74      public static final int ZONE_OFFSET         = 15;
75      public static final int DST_OFFSET          = 16;
76      public static final int FIELD_COUNT         = 17;
77  
78  
79      // fields
80      protected Date date                 = null;
81      protected int second                = -1;
82      protected int minute                = -1;
83      protected int hour                  = -1;
84      protected int dayOfMonth            = -1;
85      protected int month                 = -1;
86      protected int year                  = -1;
87      protected int amPm                  = -1;
88  
89      protected int firstWeekDayOfWeek    = Calendar.SUNDAY;
90  
91      protected Calendar() {
92          setTime(new Date());
93      }
94  
95      // Sets this Calendar's current time with the given Date.
96      public void setTime(Date date) {
97          this.date               = date;
98          computeFields();
99      }
100     public void setTime(Calendar cal) {
101         this.date               = cal.getTime();
102         computeFields();
103     }
104     // Sets this Calendar's current time from the given long value.
105     public void setTimeInMillis(long milliSeconds) {
106         this.date.setTime(milliSeconds);
107         computeFields();
108     }
109     // Sets the values for the fields year, month, and day.
110     public void set(int year, int month, int dayOfMonth) {
111         this.year               = year;
112         this.month              = month;
113         this.dayOfMonth         = dayOfMonth;
114         computeTime();
115     }
116     // Sets the values for the fields year, month, day, hour, and minute.
117     public void set(int year, int month, int dayOfMonth, int hour) {
118         this.year               = year;
119         this.month              = month;
120         this.dayOfMonth         = dayOfMonth;
121         this.hour               = hour;
122         computeTime();
123     }
124     // Sets the values for the fields year, month, day, hour, and minute.
125     public void set(int year, int month, int dayOfMonth, int hour, int minute) {
126         this.year               = year;
127         this.month              = month;
128         this.dayOfMonth         = dayOfMonth;
129         this.hour               = hour;
130         this.minute             = minute;
131         computeTime();
132     }
133     // Sets the values for the fields year, month, day, hour, minute, and second.
134     public void set(int year, int month, int dayOfMonth, int hour, int minute, int second) {
135         this.year               = year;
136         this.month              = month;
137         this.dayOfMonth         = dayOfMonth;
138         this.hour               = hour;
139         this.minute             = minute;
140         this.second             = second;
141         computeTime();
142     }
143     // Sets the time field with the given value.
144     public void set(int fieldCode, String value) {
145         set(fieldCode, Integer.parseInt(value));
146     }
147     public abstract void set(int fieldCode, int value);
148 
149     // Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
150     public void setFirstWeekDayOfWeek(int firstWeekDayOfWeek) {
151         this.firstWeekDayOfWeek = firstWeekDayOfWeek;
152     }
153 
154 
155     // Gets this Calendar's current time.
156     public Date getTime() {
157         return this.date;
158     }
159     // Gets this Calendar's current time as a long.
160     public long getTimeInMillis() {
161         return this.date.getTime();
162     }
163 
164 
165     // Gets the value for a given time field.
166     public int get(int fieldCode) {
167         switch (fieldCode) {
168         case Calendar.SECOND:
169             return this.second;
170         case Calendar.MINUTE:
171             return this.minute;
172         case Calendar.HOUR:
173             return this.hour;
174         case Calendar.HOUR_OF_DAY:
175             if (this.amPm == Calendar.PM)
176                 return this.hour + 12;
177             return this.hour;
178         case Calendar.DATE:
179             return this.dayOfMonth;
180         case Calendar.MONTH:
181             return this.month;
182         case Calendar.YEAR:
183             return this.year;
184         case Calendar.AM_PM:
185             return this.amPm;
186         default:
187             return -1;
188         }
189     }
190     // Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
191     public int getFirstWeekDayOfWeek() {
192         return this.firstWeekDayOfWeek;
193     }
194 
195 
196 
197     // Compares the time field records.
198     public boolean before(Date when) {
199         return this.date.before(when);
200     }
201     public boolean before(Calendar when) {
202         return this.date.before(when.getTime());
203     }
204     public boolean after(Date when) {
205         return this.date.after(when);
206     }
207     public boolean after(Calendar when) {
208         return this.date.after(when.getTime());
209     }
210     public boolean equals(Date obj) {
211         return this.date.equals(obj);
212     }
213     public boolean equals(Calendar obj) {
214         return this.date.equals(obj.getTime());
215     }
216     // Determines if the given time field has a value set.
217     public boolean isSet(int fieldCode) {
218         return true;
219     }
220 
221 
222     // Time Field Rolling function.
223     public void roll(int fieldCode, boolean up) {
224         if (up)
225             roll(fieldCode, 1);
226         else
227             roll(fieldCode, -1);
228     }
229 
230     // Fills in any unset fields in the time field list.
231     protected  void complete() {
232         computeFields();
233     }
234 
235     // Returns a hash code for this calendar.
236     public int hashCode() {
237         return this.date.hashCode();
238     }
239     // Return a string representation of this calendar.
240     public String toString() {
241         return this.date.toString();
242     }
243 
244 
245 
246     // Converts the current millisecond time value time to field values in fields[].
247     protected abstract void computeFields();
248 
249     // Converts the current field values in fields[] to the millisecond time value time.
250     protected abstract void computeTime();
251 
252 
253     // Gets the minimum value for the given time field.
254     public abstract int getMinimum(int fieldCode);
255     // Return the minimum value that this field could have, given the current date.
256     public abstract int getActualMinimum(int fieldCode);
257     // Gets the highest minimum value for the given field if varies.
258     public abstract int getGreatestMinimum(int fieldCode);
259 
260     // Gets the maximum value for the given time field.
261     public abstract int getMaximum(int fieldCode);
262     // Return the maximum value that this field could have, given the current date.
263     public abstract int getActualMaximum(int fieldCode);
264     // Gets the lowest maximum value for the given field if varies.
265     public abstract int getLeastMaximum(int fieldCode);
266 
267 
268     // Date Arithmetic function.
269     public abstract void add(int fieldCode, int amount);
270 
271     // Time Field Rolling function.
272     public abstract void roll(int fieldCode, int amount);
273 
274 
275 } // end of class Calendar