Source Code Cross Referenced for CalendarValidator.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » routines » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator.routines 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 org.apache.commons.validator.routines;
018:
019:        import java.text.DateFormat;
020:        import java.text.Format;
021:        import java.util.Calendar;
022:        import java.util.Locale;
023:        import java.util.TimeZone;
024:
025:        /**
026:         * <p><b>Calendar Validation</b> and Conversion routines (<code>java.util.Calendar</code>).</p>
027:         *
028:         * <p>This validator provides a number of methods for validating/converting
029:         *    a <code>String</code> date value to a <code>java.util.Calendar</code> using 
030:         *    <code>java.text.DateFormat</code> to parse either:</p>
031:         *    <ul>
032:         *       <li>using the default format for the default <code>Locale</code></li>
033:         *       <li>using a specified pattern with the default <code>Locale</code></li>
034:         *       <li>using the default format for a specified <code>Locale</code></li>
035:         *       <li>using a specified pattern with a specified <code>Locale</code></li>
036:         *    </ul>
037:         *    
038:         * <p>For each of the above mechanisms, conversion method (i.e the  
039:         *    <code>validate</code> methods) implementations are provided which
040:         *    either use the default <code>TimeZone</code> or allow the 
041:         *    <code>TimeZone</code> to be specified.</p>
042:         *
043:         * <p>Use one of the <code>isValid()</code> methods to just validate or
044:         *    one of the <code>validate()</code> methods to validate and receive a
045:         *    <i>converted</i> <code>Calendar</code> value.</p>
046:         *
047:         * <p>Implementations of the <code>validate()</code> method are provided
048:         *    to create <code>Calendar</code> objects for different <i>time zones</i>
049:         *    if the system default is not appropriate.</p>
050:         *    
051:         * <p>Alternatively the CalendarValidator's <code>adjustToTimeZone()</code> method
052:         *    can be used to adjust the <code>TimeZone</code> of the <code>Calendar</code>
053:         *    object afterwards.</p> 
054:         * 
055:         * <p>Once a value has been sucessfully converted the following
056:         *    methods can be used to perform various date comparison checks:</p>
057:         *    <ul>
058:         *       <li><code>compareDates()</code> compares the day, month and
059:         *           year of two calendars, returing 0, -1 or +1 indicating
060:         *           whether the first date is equal, before or after the second.</li>
061:         *       <li><code>compareWeeks()</code> compares the week and 
062:         *           year of two calendars, returing 0, -1 or +1 indicating
063:         *           whether the first week is equal, before or after the second.</li>
064:         *       <li><code>compareMonths()</code> compares the month and
065:         *           year of two calendars, returing 0, -1 or +1 indicating
066:         *           whether the first month is equal, before or after the second.</li>
067:         *       <li><code>compareQuarters()</code> compares the quarter and
068:         *           year of two calendars, returing 0, -1 or +1 indicating
069:         *           whether the first quarter is equal, before or after the second.</li>
070:         *       <li><code>compareYears()</code> compares the 
071:         *           year of two calendars, returing 0, -1 or +1 indicating
072:         *           whether the first year is equal, before or after the second.</li>
073:         *    </ul>
074:         * 
075:         * <p>So that the same mechanism used for parsing an <i>input</i> value 
076:         *    for validation can be used to format <i>output</i>, corresponding
077:         *    <code>format()</code> methods are also provided. That is you can 
078:         *    format either:</p>
079:         *    <ul>
080:         *       <li>using a specified pattern</li>
081:         *       <li>using the format for a specified <code>Locale</code></li>
082:         *       <li>using the format for the <i>default</i> <code>Locale</code></li>
083:         *    </ul>
084:         * 
085:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
086:         * @since Validator 1.3.0
087:         */
088:        public class CalendarValidator extends AbstractCalendarValidator {
089:
090:            private static final CalendarValidator VALIDATOR = new CalendarValidator();
091:
092:            /**
093:             * Return a singleton instance of this validator.
094:             * @return A singleton instance of the CalendarValidator.
095:             */
096:            public static CalendarValidator getInstance() {
097:                return VALIDATOR;
098:            }
099:
100:            /**
101:             * Construct a <i>strict</i> instance with <i>short</i>
102:             * date style.
103:             */
104:            public CalendarValidator() {
105:                this (true, DateFormat.SHORT);
106:            }
107:
108:            /**
109:             * Construct an instance with the specified <i>strict</i>
110:             * and <i>date style</i> parameters.
111:             * 
112:             * @param strict <code>true</code> if strict 
113:             *        <code>Format</code> parsing should be used.
114:             * @param dateStyle the date style to use for Locale validation.
115:             */
116:            public CalendarValidator(boolean strict, int dateStyle) {
117:                super (strict, dateStyle, -1);
118:            }
119:
120:            /**
121:             * <p>Validate/convert a <code>Calendar</code> using the default
122:             *    <code>Locale</code> and <code>TimeZone</code>. 
123:             *
124:             * @param value The value validation is being performed on.
125:             * @return The parsed <code>Calendar</code> if valid or <code>null</code>
126:             *  if invalid.
127:             */
128:            public Calendar validate(String value) {
129:                return (Calendar) parse(value, (String) null, (Locale) null,
130:                        (TimeZone) null);
131:            }
132:
133:            /**
134:             * <p>Validate/convert a <code>Calendar</code> using the specified
135:             *    <code>TimeZone</code> and default <code>Locale</code>.
136:             *
137:             * @param value The value validation is being performed on.
138:             * @param timeZone The Time Zone used to parse the date, system default if null.
139:             * @return The parsed <code>Calendar</code> if valid or <code>null</code>
140:             *  if invalid.
141:             */
142:            public Calendar validate(String value, TimeZone timeZone) {
143:                return (Calendar) parse(value, (String) null, (Locale) null,
144:                        timeZone);
145:            }
146:
147:            /**
148:             * <p>Validate/convert a <code>Calendar</code> using the specified
149:             *    <i>pattern</i> and default <code>TimeZone</code>.
150:             *
151:             * @param value The value validation is being performed on.
152:             * @param pattern The pattern used to validate the value against.
153:             * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
154:             */
155:            public Calendar validate(String value, String pattern) {
156:                return (Calendar) parse(value, pattern, (Locale) null,
157:                        (TimeZone) null);
158:            }
159:
160:            /**
161:             * <p>Validate/convert a <code>Calendar</code> using the specified
162:             *    <i>pattern</i> and <code>TimeZone</code>.
163:             *
164:             * @param value The value validation is being performed on.
165:             * @param pattern The pattern used to validate the value against.
166:             * @param timeZone The Time Zone used to parse the date, system default if null.
167:             * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
168:             */
169:            public Calendar validate(String value, String pattern,
170:                    TimeZone timeZone) {
171:                return (Calendar) parse(value, pattern, (Locale) null, timeZone);
172:            }
173:
174:            /**
175:             * <p>Validate/convert a <code>Calendar</code> using the specified
176:             *    <code>Locale</code> and default <code>TimeZone</code>.
177:             *
178:             * @param value The value validation is being performed on.
179:             * @param locale The locale to use for the date format, system default if null.
180:             * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
181:             */
182:            public Calendar validate(String value, Locale locale) {
183:                return (Calendar) parse(value, (String) null, locale,
184:                        (TimeZone) null);
185:            }
186:
187:            /**
188:             * <p>Validate/convert a <code>Calendar</code> using the specified
189:             *    <code>Locale</code> and <code>TimeZone</code>.
190:             *
191:             * @param value The value validation is being performed on.
192:             * @param locale The locale to use for the date format, system default if null.
193:             * @param timeZone The Time Zone used to parse the date, system default if null.
194:             * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
195:             */
196:            public Calendar validate(String value, Locale locale,
197:                    TimeZone timeZone) {
198:                return (Calendar) parse(value, (String) null, locale, timeZone);
199:            }
200:
201:            /**
202:             * <p>Validate/convert a <code>Calendar</code> using the specified pattern
203:             *    and <code>Locale</code> and the default <code>TimeZone</code>.
204:             *
205:             * @param value The value validation is being performed on.
206:             * @param pattern The pattern used to validate the value against, or the
207:             *        default for the <code>Locale</code> if <code>null</code>.
208:             * @param locale The locale to use for the date format, system default if null.
209:             * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
210:             */
211:            public Calendar validate(String value, String pattern, Locale locale) {
212:                return (Calendar) parse(value, pattern, locale, (TimeZone) null);
213:            }
214:
215:            /**
216:             * <p>Validate/convert a <code>Calendar</code> using the specified
217:             *    pattern, and <code>Locale</code> and <code>TimeZone</code>.
218:             *
219:             * @param value The value validation is being performed on.
220:             * @param pattern The pattern used to validate the value against, or the
221:             *        default for the <code>Locale</code> if <code>null</code>.
222:             * @param locale The locale to use for the date format, system default if null.
223:             * @param timeZone The Time Zone used to parse the date, system default if null.
224:             * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
225:             */
226:            public Calendar validate(String value, String pattern,
227:                    Locale locale, TimeZone timeZone) {
228:                return (Calendar) parse(value, pattern, locale, timeZone);
229:            }
230:
231:            /**
232:             * <p>Adjusts a Calendar's value to a different TimeZone.</p>
233:             * 
234:             * @param value The value to adjust.
235:             * @param timeZone The new time zone to use to adjust the Calendar to.
236:             */
237:            public static void adjustToTimeZone(Calendar value,
238:                    TimeZone timeZone) {
239:                if (value.getTimeZone().hasSameRules(timeZone)) {
240:                    value.setTimeZone(timeZone);
241:                } else {
242:                    int year = value.get(Calendar.YEAR);
243:                    int month = value.get(Calendar.MONTH);
244:                    int date = value.get(Calendar.DATE);
245:                    int hour = value.get(Calendar.HOUR_OF_DAY);
246:                    int minute = value.get(Calendar.MINUTE);
247:                    value.setTimeZone(timeZone);
248:                    value.set(year, month, date, hour, minute);
249:                }
250:            }
251:
252:            /**
253:             * <p>Compare Dates (day, month and year - not time).</p>
254:             * 
255:             * @param value The <code>Calendar</code> value to check.
256:             * @param compare The <code>Calendar</code> to compare the value to.
257:             * @return Zero if the dates are equal, -1 if first
258:             * date is less than the seconds and +1 if the first
259:             * date is greater than.
260:             */
261:            public int compareDates(Calendar value, Calendar compare) {
262:                return compare(value, compare, Calendar.DATE);
263:            }
264:
265:            /**
266:             * <p>Compare Weeks (week and year).</p>
267:             * 
268:             * @param value The <code>Calendar</code> value to check.
269:             * @param compare The <code>Calendar</code> to compare the value to.
270:             * @return Zero if the weeks are equal, -1 if first
271:             * parameter's week is less than the seconds and +1 if the first
272:             * parameter's week is greater than.
273:             */
274:            public int compareWeeks(Calendar value, Calendar compare) {
275:                return compare(value, compare, Calendar.WEEK_OF_YEAR);
276:            }
277:
278:            /**
279:             * <p>Compare Months (month and year).</p>
280:             * 
281:             * @param value The <code>Calendar</code> value to check.
282:             * @param compare The <code>Calendar</code> to compare the value to.
283:             * @return Zero if the months are equal, -1 if first
284:             * parameter's month is less than the seconds and +1 if the first
285:             * parameter's month is greater than.
286:             */
287:            public int compareMonths(Calendar value, Calendar compare) {
288:                return compare(value, compare, Calendar.MONTH);
289:            }
290:
291:            /**
292:             * <p>Compare Quarters (quarter and year).</p>
293:             * 
294:             * @param value The <code>Calendar</code> value to check.
295:             * @param compare The <code>Calendar</code> to check the value against.
296:             * @return Zero if the quarters are equal, -1 if first
297:             * parameter's quarter is less than the seconds and +1 if the first
298:             * parameter's quarter is greater than.
299:             */
300:            public int compareQuarters(Calendar value, Calendar compare) {
301:                return compareQuarters(value, compare, 1);
302:            }
303:
304:            /**
305:             * <p>Compare Quarters (quarter and year).</p>
306:             * 
307:             * @param value The <code>Calendar</code> value to check.
308:             * @param compare The <code>Calendar</code> to compare the value to.
309:             * @param monthOfFirstQuarter The  month that the first quarter starts.
310:             * @return Zero if the quarters are equal, -1 if first
311:             * parameter's quarter is less than the seconds and +1 if the first
312:             * parameter's quarter is greater than.
313:             */
314:            public int compareQuarters(Calendar value, Calendar compare,
315:                    int monthOfFirstQuarter) {
316:                return super .compareQuarters(value, compare,
317:                        monthOfFirstQuarter);
318:            }
319:
320:            /**
321:             * <p>Compare Years.</p>
322:             * 
323:             * @param value The <code>Calendar</code> value to check.
324:             * @param compare The <code>Calendar</code> to compare the value to.
325:             * @return Zero if the years are equal, -1 if first
326:             * parameter's year is less than the seconds and +1 if the first
327:             * parameter's year is greater than.
328:             */
329:            public int compareYears(Calendar value, Calendar compare) {
330:                return compare(value, compare, Calendar.YEAR);
331:            }
332:
333:            /**
334:             * <p>Convert the parsed <code>Date</code> to a <code>Calendar</code>.</p>
335:             * 
336:             * @param value The parsed <code>Date</code> object created.
337:             * @param formatter The Format used to parse the value with.
338:             * @return The parsed value converted to a <code>Calendar</code>.
339:             */
340:            protected Object processParsedValue(Object value, Format formatter) {
341:                return ((DateFormat) formatter).getCalendar();
342:            }
343:
344:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.