KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > util > ISO8601DateParser


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.roller.util;
17
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22 /**
23  * ISO 8601 date parsing utility. Designed for parsing the ISO subset used in
24  * Dublin Core, RSS 1.0, and Atom.
25  *
26  * @author <a HREF="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
27  * @version $Id: ISO8601DateParser.java,v 1.2 2005/06/03 20:25:29 snoopdave Exp $
28  */

29 public class ISO8601DateParser {
30
31     // 2004-06-14T19:GMT20:30Z
32
// 2004-06-20T06:GMT22:01Z
33

34     private static SimpleDateFormat JavaDoc df
35         = new SimpleDateFormat JavaDoc( "yyyy-MM-dd'T'HH:mm:ssz" );
36
37     // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
38
//
39
// http://www.intertwingly.net/wiki/pie/DateTime
40
//
41
// http://www.w3.org/TR/NOTE-datetime
42
//
43
// Different standards may need different levels of granularity in the date and
44
// time, so this profile defines six levels. Standards that reference this
45
// profile should specify one or more of these granularities. If a given
46
// standard allows more than one granularity, it should specify the meaning of
47
// the dates and times with reduced precision, for example, the result of
48
// comparing two dates with different precisions.
49

50     // The formats are as follows. Exactly the components shown here must be
51
// present, with exactly this punctuation. Note that the "T" appears literally
52
// in the string, to indicate the beginning of the time element, as specified in
53
// ISO 8601.
54

55     // Year:
56
// YYYY (eg 1997)
57
// Year and month:
58
// YYYY-MM (eg 1997-07)
59
// Complete date:
60
// YYYY-MM-DD (eg 1997-07-16)
61
// Complete date plus hours and minutes:
62
// YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
63
// Complete date plus hours, minutes and seconds:
64
// YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
65
// Complete date plus hours, minutes, seconds and a decimal fraction of a
66
// second
67
// YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
68

69     // where:
70

71     // YYYY = four-digit year
72
// MM = two-digit month (01=January, etc.)
73
// DD = two-digit day of month (01 through 31)
74
// hh = two digits of hour (00 through 23) (am/pm NOT allowed)
75
// mm = two digits of minute (00 through 59)
76
// ss = two digits of second (00 through 59)
77
// s = one or more digits representing a decimal fraction of a second
78
// TZD = time zone designator (Z or +hh:mm or -hh:mm)
79
public static Date JavaDoc parse( String JavaDoc input ) throws java.text.ParseException JavaDoc {
80
81         //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
82
//things a bit. Before we go on we have to repair this.
83

84         //this is zero time so we need to add that TZ indicator for
85
if ( input.endsWith( "Z" ) ) {
86             input = input.substring( 0, input.length() - 1) + "GMT-00:00";
87         } else {
88             int inset = 6;
89         
90             String JavaDoc s0 = input.substring( 0, input.length() - inset );
91             String JavaDoc s1 = input.substring( input.length() - inset, input.length() );
92
93             input = s0 + "GMT" + s1;
94         }
95         
96         return df.parse( input );
97         
98     }
99
100     public static String JavaDoc toString( Date JavaDoc date ) {
101
102         TimeZone JavaDoc tz = TimeZone.getTimeZone( "UTC" );
103         
104         df.setTimeZone( tz );
105
106         String JavaDoc output = df.format( date );
107
108         int inset0 = 9;
109         int inset1 = 6;
110         
111         String JavaDoc s0 = output.substring( 0, output.length() - inset0 );
112         String JavaDoc s1 = output.substring( output.length() - inset1, output.length() );
113
114         String JavaDoc result = s0 + s1;
115
116         result = result.replaceAll( "UTC", "+00:00" );
117         
118         return result;
119         
120     }
121
122 }
Popular Tags