KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > contrib > beans > DateUtils


1 /*--
2
3  $Id: DateUtils.java,v 1.2 2004/02/06 09:57:48 jhunter Exp $
4
5  Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54
55  */

56
57 // Based on code Copyright (c) 1998-2000 Alex Chaffee and Purple Technology.
58

59 package org.jdom.contrib.beans;
60
61 import java.util.*;
62 import org.apache.regexp.*;
63 import java.text.*;
64 import java.io.PrintStream JavaDoc;
65
66 /**
67  * @author Alex Chaffee (alex@jguru.com)
68  **/

69 public class DateUtils {
70
71     public static boolean debug;
72     
73     /**
74      * Tries to parse the date according to several different formats.
75      * <p>
76      * BUG in TimeZone processing -- Calendar class always screws up the time when a TZ is set -- so ignored for now.
77      *
78      * @return null if not parseable
79      **/

80     public static Date parseDate(String JavaDoc s) {
81     Date date = null;
82
83     // some standard date format
84
try {
85         // this is deprecated, but it still parses more
86
// formats than DateFormat.parse(String)
87
date = new Date(s);
88         return date;
89     }
90     catch (IllegalArgumentException JavaDoc dfe) {
91     }
92
93     // some other (?) standard date format
94
try {
95         date = DateFormat.getDateInstance().parse(s);
96         return date;
97     }
98     catch (ParseException pe) {
99     }
100         
101     // a single int = msec since 1970
102
try {
103         long secs = Long.parseLong(s);
104         date = new Date(s);
105         return date;
106     }
107     catch (NumberFormatException JavaDoc nfe) {
108     }
109
110     ISO8601 iso = parseISO8601(s);
111     if (iso != null) {
112         TimeZone tz = null;
113         /*
114         // see if setting tz first fixes tz bug
115         if (iso.tz != null && !(iso.tz.length()==0)) {
116             if (iso.tz.equals("Z"))
117             tz = TimeZone.getTimeZone("GMT");
118             else if (iso.tz.length()==3)
119             tz = TimeZone.getTimeZone(iso.tz);
120             else
121             tz = TimeZone.getTimeZone("GMT" + iso.tz);
122         }
123         */

124
125         Calendar cal;
126         if (tz == null)
127             cal = Calendar.getInstance();
128         else
129             cal = Calendar.getInstance(tz);
130
131         cal.set(Calendar.YEAR, iso.year);
132         cal.set(Calendar.MONTH, iso.month - 1);
133         cal.set(Calendar.DAY_OF_MONTH, iso.day);
134         cal.set(Calendar.HOUR, iso.hour + 12); // ??? TZ bug again?
135
cal.set(Calendar.MINUTE, iso.min);
136         cal.set(Calendar.SECOND, iso.sec);
137         
138         return cal.getTime(); // why the hell does getTime() return a Date?
139

140
141     } // if iso
142

143     return null;
144     } // parseDate
145

146     public static class ISO8601 {
147     public int year;
148     public int month;
149     public int day;
150     public int hour;
151     public int min;
152     public int sec;
153     public int frac;
154     public String JavaDoc tz;
155     }
156
157     protected static String JavaDoc reISO8601 =
158     "(\\d\\d\\d\\d)(-(\\d\\d)(-(\\d\\d))?)?" +
159     "([T| ]?" +
160     "(\\d\\d):(\\d\\d)(:((\\d\\d)(\\.(\\d+))?)?)?" +
161     "(Z|([+-]\\d\\d:\\d\\d)|([A-Z]{3}))?)?";
162     
163     public static ISO8601 parseISO8601(String JavaDoc s) {
164     // ISO 8601 datetime: http://www.w3.org/TR/NOTE-datetime
165
// e.g. 1997-07-16T19:20:30.45+01:00
166
// additions: "T" can be a space, TZ can be a three-char code, TZ can be missing
167
try {
168         RE re = new RE(reISO8601);
169         if (re.match(s)) {
170         if (debug)
171             showParens(re);
172         
173         ISO8601 iso = new ISO8601();
174         iso.year = toInt(re.getParen(1));
175         iso.month = toInt(re.getParen(3));
176         iso.day = toInt(re.getParen(5));
177         iso.hour = toInt(re.getParen(7));
178         iso.min = toInt(re.getParen(8));
179         iso.sec = toInt(re.getParen(11));
180         iso.frac = toInt(re.getParen(13));
181         iso.tz = re.getParen(14);
182
183         if (debug) {
184             System.out.println("year='" + iso.year + "'");
185             System.out.println("month='" + iso.month + "'");
186             System.out.println("day='" + iso.day + "'");
187             System.out.println("hour='" + iso.hour + "'");
188             System.out.println("min='" + iso.min + "'");
189             System.out.println("sec='" + iso.sec + "'");
190             System.out.println("frac='" + iso.frac + "'");
191             System.out.println("tz='" + iso.tz + "'");
192         }
193
194         return iso;
195         }
196     } // try
197
catch (RESyntaxException ree) {
198         ree.printStackTrace();
199     }
200     return null;
201     }
202
203     public static int toInt(String JavaDoc x) {
204     if (x == null) return 0;
205     try {
206         return Integer.parseInt(x);
207     }
208     catch (NumberFormatException JavaDoc e) {
209         return 0;
210     }
211     }
212     
213     /**
214      * Dump parenthesized subexpressions found by a regular expression matcher object
215      * @param r Matcher object with results to show
216     */

217     static void showParens(RE r)
218     {
219         // Loop through each paren
220
for (int i = 0; i < r.getParenCount(); i++)
221         {
222             // Show paren register
223
System.out.println("$" + i + " = " + r.getParen(i));
224         }
225     }
226
227     public static void main(String JavaDoc[] args) {
228     debug = true;
229     for (int i=0; i<args.length; ++i) {
230         System.out.println( parseDate(args[i]) );
231     }
232     }
233 }
234
Popular Tags