KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > swing > datefield > CalendarAdaptor


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.swing.datefield;
20
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23
24 /**
25  * Adaptor for the {@link java.util.Calendar} class. This fixes issues with the
26  * way in which months are handled (January=0) and also provides a way of
27  * setting and retrieving values by String.
28  *
29  * @author Matthew Large
30  * @version $Revision: 1.1 $
31  *
32  */

33 public class CalendarAdaptor {
34
35     /**
36      * Calendar that this adaptor is wrapping.
37      */

38     private Calendar JavaDoc m_calendar = null;
39
40     /**
41      *
42      */

43     public CalendarAdaptor() {
44         super();
45         this.m_calendar = Calendar.getInstance();
46     }
47     
48     
49
50     /**
51      * Gets the value for a given time field.
52      *
53      * @param nField The given time field
54      * @return The value for the given time field
55      * @see Calendar#get(int)
56      */

57     public int get(int nField) {
58         if(nField==Calendar.MONTH) {
59             return m_calendar.get(nField)+1;
60         } else {
61             return m_calendar.get(nField);
62         }
63     }
64     
65     /**
66      * Gets the value for a given time field.
67      *
68      * @param nField The given time field
69      * @return The value for the given time field
70      * @see Calendar#get(int)
71      */

72     public String JavaDoc getStringValue(int nField) {
73         int nValue = this.get(nField);
74         if(nField==Calendar.ERA) {
75             if(nValue==0) {
76                 return "BC";
77             } else {
78                 return "AD";
79             }
80         } else if(nField!=Calendar.YEAR) {
81             if(nValue<10) {
82                 return "0" + Integer.toString( nValue );
83             } else {
84                 return Integer.toString(nValue);
85             }
86         } else if(nField==Calendar.YEAR) {
87             String JavaDoc sVal = Integer.toString(nValue);
88             StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
89             if(sVal.length()<8) {
90                  sBuff.append(" ");
91             }
92             if(sVal.length()<7) {
93                  sBuff.append(" ");
94             }
95             if(sVal.length()<6) {
96                  sBuff.append(" ");
97             }
98             if(sVal.length()<5) {
99                  sBuff.append(" ");
100             }
101             if(sVal.length()<4) {
102                  sBuff.append("0");
103             }
104             if(sVal.length()<3) {
105                  sBuff.append("0");
106             }
107             if(sVal.length()<2) {
108                  sBuff.append("0");
109             }
110             sBuff.append(sVal);
111             return sBuff.toString();
112         }
113         return null;
114     }
115
116     /**
117      * Return the maximum value that this field could have, given the current
118      * date. For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field,
119      * the actual maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for
120      * a Hebrew calendar, for some years the actual maximum for MONTH is 12, and
121      * for others 13. The version of this function on Calendar uses an iterative
122      * algorithm to determine the actual maximum value for the field. There is
123      * almost always a more efficient way to accomplish this (in most cases, you
124      * can simply return getMaximum()). GregorianCalendar overrides this function
125      * with a more efficient implementation.
126      *
127      * @param nField The field to determine the maximum of
128      * @return The maximum of the given field for the current date of this Calendar
129      * @see Calendar#getActualMaximum(int)
130      */

131     public int getActualMaximum(int nField) {
132         return m_calendar.getActualMaximum(nField);
133     }
134
135     /**
136      * Return the minimum value that this field could have, given the current
137      * date. For the Gregorian calendar, this is the same as getMinimum() and
138      * getGreatestMinimum(). The version of this function on Calendar uses an
139      * iterative algorithm to determine the actual minimum value for the field.
140      * There is almost always a more efficient way to accomplish this (in most
141      * cases, you can simply return getMinimum()). GregorianCalendar overrides
142      * this function with a more efficient implementation.
143      *
144      * @param nField The field to determine the minimum of
145      * @return The minimum of the given field for the current date of this Calendar
146      * @see Calendar#getActualMinimum(int)
147      */

148     public int getActualMinimum(int nField) {
149         return m_calendar.getActualMinimum(nField);
150     }
151
152     /**
153      * Gets the maximum value for the given time field. e.g. for Gregorian
154      * DAY_OF_MONTH, 31.
155      *
156      * @param nField The given time field
157      * @return The maximum value for the given time field
158      * @see Calendar#getMaximum(int)
159      */

160     public int getMaximum(int nField) {
161         return m_calendar.getMaximum(nField);
162     }
163
164     /**
165      * Gets the minimum value for the given time field. e.g., for Gregorian
166      * DAY_OF_MONTH, 1.
167      *
168      * @param nField The given time field
169      * @return The minimum value for the given time field
170      * @see Calendar#getMinimum(int)
171      */

172     public int getMinimum(int nField) {
173         return m_calendar.getMinimum(nField);
174     }
175
176     /**
177      * Sets the time field with the given value. Fixes the problem
178      * with month values, this method assumes January=1.
179      *
180      * @param nField The given time field
181      * @param nValue The value to be set for the given time field
182      * @see Calendar#set(int, int)
183      */

184     public void set(int nField, int nValue) {
185         if(nField==Calendar.MONTH) {
186             m_calendar.set(nField, nValue-1);
187         } else {
188             m_calendar.set(nField, nValue);
189         }
190     }
191     
192     /**
193      * Sets the time field with the given value. Fixes the problem
194      * with month values, this method assumes January="1". Also uses
195      * "BC" and "AD" for the Era field instead of "0" and "1".
196      *
197      * @param nField The given time field
198      * @param sValue The value to be set for the given time field
199      * @see Calendar#set(int, int)
200      */

201     public void setStringValue(int nField, String JavaDoc sValue) {
202         if(nField==Calendar.ERA) {
203             if(sValue.equalsIgnoreCase("bc")) {
204                 this.set(nField, 0);
205             } else {
206                 this.set(nField, 1);
207             }
208         } else {
209             this.set(nField, Integer.parseInt(sValue.trim()));
210         }
211     }
212
213     /**
214      * Gets this Calendar's current time.
215      *
216      * @return The current time
217      * @see Calendar#getTime()
218      */

219     public Date JavaDoc getTime() {
220         return m_calendar.getTime();
221     }
222
223     /**
224      * Sets this Calendar's current time with the given Date.
225      *
226      * Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get().
227      *
228      * @param arg0 The given Date
229      * @see Calendar#setTime(java.util.Date)
230      */

231     public void setTime(Date JavaDoc arg0) {
232         m_calendar.setTime(arg0);
233     }
234
235     /**
236      * Date Arithmetic function. Adds the specified (signed) amount of time
237      * to the given time field, based on the calendar's rules. For example,
238      * to subtract 5 days from the current time of the calendar, you can achieve
239      * it by calling:
240      *
241      * add(Calendar.DATE, -5).
242      *
243      * @param arg0 The time field
244      * @param arg1 The amount of date or time to be added to the field
245      * @see Calendar#add(int, int)
246      */

247     public void add(int arg0, int arg1) {
248         m_calendar.add(arg0, arg1);
249     }
250
251 }
252
Popular Tags