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