KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > util > DateHelper


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.util;
5
6 import java.text.ParseException JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.Calendar JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.TimeZone JavaDoc;
11
12 import org.oddjob.OJConstants;
13
14 /**
15  * Date Helper Utilities
16  *
17  * @author Rob Gordon.
18  */

19 public class DateHelper {
20
21     /**
22      * Parse a date and time. The input can either be just a date
23      * or a date and a time.
24      *
25      * @param text The date time.
26      * @return The date for the given text in the current time zone.
27      * @throws ParseException If the text isn't in a recognized
28      * date/time foramt.
29      */

30     public static Date JavaDoc parseDateTime(String JavaDoc text) throws ParseException JavaDoc {
31         return parseDateTime(text, TimeZone.getDefault());
32     }
33     
34     /**
35      * Parse a date and time in the given time zone. The input can
36      * either be just a date or a date and time.
37      *
38      * @param text The date time.
39      * @param timeZoneId The time zone identifier.
40      * @return The date for the given text in the specified time zone.
41      * @throws ParseException If the text isn't in a recognized
42      * date/time foramt.
43      */

44     public static Date JavaDoc parseDateTime(String JavaDoc text, String JavaDoc timeZoneId)
45     throws ParseException JavaDoc {
46         TimeZone JavaDoc timeZone = TimeZone.getDefault();
47         if (timeZoneId != null) {
48             timeZone = TimeZone.getTimeZone(timeZoneId);
49         }
50         return parseDateTime(text, timeZone);
51     }
52     
53     /**
54      * Parse a date and time in the given time zone. The input can
55      * either be just a date or a date and time.
56      *
57      * @param text The date time
58      * @param timeZone The timeZone.
59      * @return The date for the given text in the specified time zone.
60      * @throws ParseException If the text isn't in a recognized
61      * date/time foramt.
62      */

63     public static Date JavaDoc parseDateTime(String JavaDoc text, TimeZone JavaDoc timeZone)
64     throws ParseException JavaDoc {
65         if (text.indexOf(' ') == -1) {
66             return parseDate(text, timeZone);
67         }
68         else {
69             try {
70                 return parse(text, OJConstants.DATE_FORMAT + " "
71                         + OJConstants.TIME_FORMAT1, timeZone);
72             } catch (ParseException JavaDoc e) {
73                 try {
74                     return parse(text, OJConstants.DATE_FORMAT + " "
75                             + OJConstants.TIME_FORMAT2, timeZone);
76                 } catch (ParseException JavaDoc e2) {
77                     return parse(text, OJConstants.DATE_FORMAT + " "
78                             + OJConstants.TIME_FORMAT3, timeZone);
79                 }
80             }
81         }
82     }
83
84     /**
85      * Parse a date using the default time zone.
86      *
87      * @param text A date.
88      * @return The date for the given text.
89      * @throws ParseException If the date isn't in the recognized
90      * date format.
91      */

92     public static Date JavaDoc parseDate(String JavaDoc text) throws ParseException JavaDoc {
93         return parseDate(text, TimeZone.getDefault());
94     }
95     
96     /**
97      * Parse a date using the given time zone.
98      *
99      * @param text The date text.
100      * @param timeZoneId The time zone identifier.
101      *
102      * @return The date for the given text in the specified time zone.
103      * @throws ParseException If the date isn't in the recognized
104      * date format.
105      */

106     public static Date JavaDoc parseDate(String JavaDoc text, String JavaDoc timeZoneId) throws ParseException JavaDoc {
107         TimeZone JavaDoc timeZone = TimeZone.getDefault();
108         if (timeZoneId != null) {
109             timeZone = TimeZone.getTimeZone(timeZoneId);
110         }
111         return parseDate(text, timeZone);
112     }
113     
114     /**
115      * Parse a date using the given time zone.
116      *
117      * @param text The date text.
118      * @param timeZone The time zone.
119      * @return The date for the given text in the specified time zone.
120      * @throws ParseException If the date isn't in the recognized
121      * date format.
122      */

123     public static Date JavaDoc parseDate(String JavaDoc text, TimeZone JavaDoc timeZone) throws ParseException JavaDoc {
124         return parse(text, OJConstants.DATE_FORMAT, timeZone);
125     }
126     
127     /**
128      * Parse a time into a number of milliseconds.
129      *
130      * @param text The time.
131      * @return The time as milliseconds.
132      * @throws ParseException If pasing fails.
133      */

134     public static long parseTime(String JavaDoc text) throws ParseException JavaDoc {
135         TimeZone JavaDoc timeZone = TimeZone.getTimeZone("GMT+00");
136         Date JavaDoc d = null;
137         try {
138             d = parse(text, OJConstants.TIME_FORMAT1, timeZone);
139         } catch (ParseException JavaDoc e) {
140             try {
141                 d = parse(text, OJConstants.TIME_FORMAT2, timeZone);
142             } catch (ParseException JavaDoc e2) {
143                     d = parse(text, OJConstants.TIME_FORMAT3, timeZone);
144             }
145         }
146         return d.getTime();
147     }
148     
149     /**
150      * Format a date into just text representing just the date.
151      *
152      * @param date The date
153      * @return The text equivelant.
154      */

155     public static String JavaDoc formatDate(Date JavaDoc date) {
156         return new SimpleDateFormat JavaDoc(OJConstants.DATE_FORMAT).format(date);
157     }
158     
159     
160     /**
161      * Helper function. Not public.
162      *
163      * @param text
164      * @param format
165      * @param timeZone
166      * @return
167      * @throws ParseException
168      */

169     static Date JavaDoc parse(String JavaDoc text, String JavaDoc format, TimeZone JavaDoc timeZone) throws ParseException JavaDoc {
170         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc(format);
171         f.setTimeZone(timeZone);
172
173         Date JavaDoc d = f.parse(text);
174         
175         Calendar JavaDoc c = Calendar.getInstance();
176         c.setTimeZone(timeZone);
177         c.setTime(d);
178         
179         int year = c.get(Calendar.YEAR);
180         if (year < 50) {
181             year = year + 2000;
182         }
183         else if (year < 100) {
184             year = year + 1900;
185         }
186         
187         c.set(Calendar.YEAR, year);
188         return c.getTime();
189     }
190     
191 }
192
193
194
Popular Tags