KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > util > ISO8601


1 /*
2  * $Id: ISO8601.java,v 1.2 2004/07/24 00:16:24 benjmestrallet Exp $
3  *
4  * Copyright 2002-2004 Day Management AG, Switzerland.
5  *
6  * Licensed under the Day RI License, Version 2.0 (the "License"),
7  * as a reference implementation of the following specification:
8  *
9  * Content Repository API for Java Technology, revision 0.12
10  * <http://www.jcp.org/en/jsr/detail?id=170>
11  *
12  * You may not use this file except in compliance with the License.
13  * You may obtain a copy of the License files at
14  *
15  * http://www.day.com/content/en/licenses/day-ri-license-2.0
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24 package javax.jcr.util;
25
26 import java.text.DecimalFormat JavaDoc;
27 import java.text.ParsePosition JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.TimeZone JavaDoc;
32
33 /**
34  * The <code>ISO8601</code> utility class provides helper methods
35  * to deal with date/time formatting using a specific ISO8601-compliant
36  * format (see <a HREF="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a>).
37  * <p/>
38  * Currently we only support the format <code>yyyy-mm-ddThh:mm:ss.SSSTZD</code>,
39  * which includes the complete date plus hours, minutes, seconds and a decimal
40  * fraction of a second. The time zone designator, <code>TZD</code>, is either
41  * <code>Z</code> for Zulu, i.e. UTC, or an offset from UTC in the form of
42  * <code>+hh:mm</code> or <code>-hh:mm</code>.
43  *
44  * @author Stefan Guggisberg
45  */

46 public final class ISO8601 {
47   /**
48    * used to fomat time zone offset
49    */

50   private static DecimalFormat JavaDoc xxFormat = new DecimalFormat JavaDoc("00");
51
52   /**
53    * specific ISO8601-compliant format string (without time zone information)
54    */

55   private static String JavaDoc ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
56
57   /**
58    * Parses a ISO8601-compliant date/time string.
59    *
60    * @param text the date/time string to be parsed
61    * @return a <code>Calendar</code>, or <code>null</code> if the input could
62    * not be parsed
63    */

64   public static Calendar JavaDoc parse(String JavaDoc text) {
65 // parse time zone designator (Z or +00:00 or -00:00)
66
// and build time zone id (GMT or GMT+00:00 or GMT-00:00)
67
String JavaDoc tzID = "GMT"; // Zulu, i.e. UTC/GMT (default)
68
int tzPos = text.indexOf('Z');
69     if (tzPos == -1) {
70       // not Zulu, try +
71
tzPos = text.indexOf('+');
72       if (tzPos == -1) {
73         // not +, try -, but remember it might be used within first
74
// 8 charaters for separating year, month and day, yyyy-mm-dd
75
tzPos = text.indexOf('-', 8);
76       }
77       if (tzPos == -1) {
78         // no time zone specified, assume Zulu
79
} else {
80         // offset to UTC specified in the format +00:00/-00:00
81
tzID += text.substring(tzPos);
82         text = text.substring(0, tzPos);
83       }
84     } else {
85       // Zulu, i.e. UTC/GMT
86
text = text.substring(0, tzPos);
87     }
88
89     TimeZone JavaDoc tz = TimeZone.getTimeZone(tzID);
90     SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(ISO_FORMAT);
91     format.setLenient(false);
92     format.setTimeZone(tz);
93     Date JavaDoc date = format.parse(text, new ParsePosition JavaDoc(0));
94     if (date == null) {
95       return null;
96     }
97     Calendar JavaDoc cal = Calendar.getInstance(tz);
98     cal.setTime(date);
99     return cal;
100   }
101
102   /**
103    * Formats a <code>Calendar</code> value into a ISO8601-compliant
104    * date/time string.
105    *
106    * @param cal the time value to be formatted into a date/time string.
107    * @return the formatted date/time string.
108    */

109   public static String JavaDoc format(Calendar JavaDoc cal) {
110     SimpleDateFormat JavaDoc format =
111         new SimpleDateFormat JavaDoc(ISO_FORMAT);
112     TimeZone JavaDoc tz = cal.getTimeZone();
113     format.setTimeZone(tz);
114
115     StringBuffer JavaDoc tzd = new StringBuffer JavaDoc();
116     int offset = tz.getRawOffset();
117     if (offset != 0) {
118       int hours = Math.abs((offset / (60 * 1000)) / 60);
119       int minutes = Math.abs((offset / (60 * 1000)) % 60);
120       tzd.append(offset < 0 ? "-" : "+");
121       tzd.append(xxFormat.format(hours));
122       tzd.append(":");
123       tzd.append(xxFormat.format(minutes));
124     } else {
125       tzd.append("Z");
126     }
127     return format.format(cal.getTime()) + tzd.toString();
128   }
129 }
130
Popular Tags