001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.roller.util;
017:
018: import java.text.SimpleDateFormat;
019: import java.util.Date;
020: import java.util.TimeZone;
021:
022: /**
023: * ISO 8601 date parsing utility. Designed for parsing the ISO subset used in
024: * Dublin Core, RSS 1.0, and Atom.
025: *
026: * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
027: * @version $Id: ISO8601DateParser.java,v 1.2 2005/06/03 20:25:29 snoopdave Exp $
028: */
029: public class ISO8601DateParser {
030:
031: // 2004-06-14T19:GMT20:30Z
032: // 2004-06-20T06:GMT22:01Z
033:
034: // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
035: //
036: // http://www.intertwingly.net/wiki/pie/DateTime
037: //
038: // http://www.w3.org/TR/NOTE-datetime
039: //
040: // Different standards may need different levels of granularity in the date and
041: // time, so this profile defines six levels. Standards that reference this
042: // profile should specify one or more of these granularities. If a given
043: // standard allows more than one granularity, it should specify the meaning of
044: // the dates and times with reduced precision, for example, the result of
045: // comparing two dates with different precisions.
046:
047: // The formats are as follows. Exactly the components shown here must be
048: // present, with exactly this punctuation. Note that the "T" appears literally
049: // in the string, to indicate the beginning of the time element, as specified in
050: // ISO 8601.
051:
052: // Year:
053: // YYYY (eg 1997)
054: // Year and month:
055: // YYYY-MM (eg 1997-07)
056: // Complete date:
057: // YYYY-MM-DD (eg 1997-07-16)
058: // Complete date plus hours and minutes:
059: // YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
060: // Complete date plus hours, minutes and seconds:
061: // YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
062: // Complete date plus hours, minutes, seconds and a decimal fraction of a
063: // second
064: // YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
065:
066: // where:
067:
068: // YYYY = four-digit year
069: // MM = two-digit month (01=January, etc.)
070: // DD = two-digit day of month (01 through 31)
071: // hh = two digits of hour (00 through 23) (am/pm NOT allowed)
072: // mm = two digits of minute (00 through 59)
073: // ss = two digits of second (00 through 59)
074: // s = one or more digits representing a decimal fraction of a second
075: // TZD = time zone designator (Z or +hh:mm or -hh:mm)
076: public static Date parse(String input)
077: throws java.text.ParseException {
078:
079: //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
080: //things a bit. Before we go on we have to repair this.
081: SimpleDateFormat df = new SimpleDateFormat(
082: "yyyy-MM-dd'T'HH:mm:ssz");
083:
084: //this is zero time so we need to add that TZ indicator for
085: if (input.endsWith("Z")) {
086: input = input.substring(0, input.length() - 1)
087: + "GMT-00:00";
088: } else {
089: int inset = 6;
090:
091: String s0 = input.substring(0, input.length() - inset);
092: String s1 = input.substring(input.length() - inset, input
093: .length());
094:
095: input = s0 + "GMT" + s1;
096: }
097:
098: return df.parse(input);
099:
100: }
101:
102: public static String toString(Date date) {
103:
104: SimpleDateFormat df = new SimpleDateFormat(
105: "yyyy-MM-dd'T'HH:mm:ssz");
106:
107: TimeZone tz = TimeZone.getTimeZone("UTC");
108:
109: df.setTimeZone(tz);
110:
111: String output = df.format(date);
112:
113: int inset0 = 9;
114: int inset1 = 6;
115:
116: String s0 = output.substring(0, output.length() - inset0);
117: String s1 = output.substring(output.length() - inset1, output
118: .length());
119:
120: String result = s0 + s1;
121:
122: result = result.replaceAll("UTC", "+00:00");
123:
124: return result;
125:
126: }
127:
128: }
|