KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > 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.apache.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     // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
35
//
36
// http://www.intertwingly.net/wiki/pie/DateTime
37
//
38
// http://www.w3.org/TR/NOTE-datetime
39
//
40
// Different standards may need different levels of granularity in the date and
41
// time, so this profile defines six levels. Standards that reference this
42
// profile should specify one or more of these granularities. If a given
43
// standard allows more than one granularity, it should specify the meaning of
44
// the dates and times with reduced precision, for example, the result of
45
// comparing two dates with different precisions.
46

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

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

66     // where:
67

68     // YYYY = four-digit year
69
// MM = two-digit month (01=January, etc.)
70
// DD = two-digit day of month (01 through 31)
71
// hh = two digits of hour (00 through 23) (am/pm NOT allowed)
72
// mm = two digits of minute (00 through 59)
73
// ss = two digits of second (00 through 59)
74
// s = one or more digits representing a decimal fraction of a second
75
// TZD = time zone designator (Z or +hh:mm or -hh:mm)
76
public static Date JavaDoc parse( String JavaDoc input ) throws java.text.ParseException JavaDoc {
77
78         //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
79
//things a bit. Before we go on we have to repair this.
80
SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc( "yyyy-MM-dd'T'HH:mm:ssz" );
81         
82         //this is zero time so we need to add that TZ indicator for
83
if ( input.endsWith( "Z" ) ) {
84             input = input.substring( 0, input.length() - 1) + "GMT-00:00";
85         } else {
86             int inset = 6;
87         
88             String JavaDoc s0 = input.substring( 0, input.length() - inset );
89             String JavaDoc s1 = input.substring( input.length() - inset, input.length() );
90
91             input = s0 + "GMT" + s1;
92         }
93         
94         return df.parse( input );
95         
96     }
97
98     public static String JavaDoc toString( Date JavaDoc date ) {
99         
100         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc( "yyyy-MM-dd'T'HH:mm:ssz" );
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