001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.parser;
019:
020: import java.text.DateFormat;
021: import java.text.ParseException;
022: import java.text.SimpleDateFormat;
023: import java.util.Calendar;
024: import java.util.Date;
025: import java.util.TimeZone;
026:
027: import net.fortuna.ical4j.util.TimeZones;
028:
029: public class DateParser {
030:
031: private static final String DEFAULT_PATTERN = "yyyyMMdd'T'HHmmss";
032:
033: private static final String UTC_PATTERN = "yyyyMMdd'T'HHmmss'Z'";
034:
035: /**
036: * local date-time representation.
037: */
038: private static DateFormat defaultFormat = new SimpleDateFormat(
039: DEFAULT_PATTERN);
040:
041: /**
042: * UTC date-time representation.
043: */
044: private static DateFormat utcFormat = new SimpleDateFormat(
045: UTC_PATTERN);
046: {
047: utcFormat.setTimeZone(TimeZone.getTimeZone(TimeZones.UTC_ID));
048: }
049:
050: public DateParser() {
051: super ();
052: }
053:
054: public static String createStringFromCalendar(Calendar calendar) {
055: return DateFormat.getInstance().format(calendar.getTime());
056: }
057:
058: public static String createDateStringFromCalendar(Calendar calendar) {
059: long millis = calendar.getTimeInMillis();
060: Date date = new Date(millis);
061: StringBuffer b = new StringBuffer(utcFormat.format(date));
062:
063: // TODO fix timezone
064: // b.append('T');
065: // Time time = new Time(millis, TimeZone.getDefault());
066: // b.append(time.toString());
067: return b.toString();
068:
069: }
070:
071: public static Calendar createCalendarFromDateString(
072: String dateString) {
073:
074: if (dateString == null)
075: throw new IllegalArgumentException("dateString == null");
076:
077: if (dateString.length() == 0)
078: throw new IllegalArgumentException(
079: "dateString.length() == 0");
080:
081: long time = -1;
082: try {
083: time = utcFormat.parse(dateString).getTime();
084: } catch (ParseException pe) {
085: defaultFormat.setTimeZone(TimeZone.getDefault());
086: try {
087: time = defaultFormat.parse(dateString).getTime();
088: } catch (ParseException e) {
089: e.printStackTrace();
090: }
091: }
092:
093: if (time == -1)
094: return null;
095: Calendar cal = Calendar.getInstance();
096: cal.setTimeInMillis(time);
097: return cal;
098:
099: }
100:
101: public static Calendar createCalendarFromString(String str) {
102: if (str == null)
103: throw new IllegalArgumentException("str == null");
104:
105: if (str.length() == 0)
106: throw new IllegalArgumentException("str.length() == 0");
107:
108: Calendar c = Calendar.getInstance();
109:
110: try {
111: Date d = DateFormat.getInstance().parse(str);
112: c.setTime(d);
113: } catch (ParseException e) {
114: e.printStackTrace();
115: }
116:
117: return c;
118: }
119:
120: }
|