KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > parser > DateParser


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.parser;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.TimeZone JavaDoc;
26
27 import net.fortuna.ical4j.util.TimeZones;
28
29 public class DateParser {
30
31     private static final String JavaDoc DEFAULT_PATTERN = "yyyyMMdd'T'HHmmss";
32
33     private static final String JavaDoc UTC_PATTERN = "yyyyMMdd'T'HHmmss'Z'";
34
35     /**
36      * local date-time representation.
37      */

38     private static DateFormat JavaDoc defaultFormat = new SimpleDateFormat JavaDoc(
39             DEFAULT_PATTERN);
40
41     /**
42      * UTC date-time representation.
43      */

44     private static DateFormat JavaDoc utcFormat = new SimpleDateFormat JavaDoc(UTC_PATTERN);
45     {
46         utcFormat.setTimeZone(TimeZone.getTimeZone(TimeZones.UTC_ID));
47     }
48
49     public DateParser() {
50         super();
51     }
52
53     public static String JavaDoc createStringFromCalendar(Calendar JavaDoc calendar) {
54         return DateFormat.getInstance().format(calendar.getTime());
55     }
56
57     public static String JavaDoc createDateStringFromCalendar(Calendar JavaDoc calendar) {
58         long millis = calendar.getTimeInMillis();
59         Date JavaDoc date = new Date JavaDoc(millis);
60         StringBuffer JavaDoc b = new StringBuffer JavaDoc(utcFormat.format(date));
61
62         // TODO fix timezone
63
// b.append('T');
64
// Time time = new Time(millis, TimeZone.getDefault());
65
// b.append(time.toString());
66
return b.toString();
67
68     }
69
70     public static Calendar JavaDoc createCalendarFromDateString(String JavaDoc dateString) {
71
72         if (dateString == null)
73             throw new IllegalArgumentException JavaDoc("dateString == null");
74
75         if (dateString.length() == 0)
76             throw new IllegalArgumentException JavaDoc("dateString.length() == 0");
77
78         long time = -1;
79         try {
80             time = utcFormat.parse(dateString).getTime();
81         } catch (ParseException JavaDoc pe) {
82             defaultFormat.setTimeZone(TimeZone.getDefault());
83             try {
84                 time = defaultFormat.parse(dateString).getTime();
85             } catch (ParseException JavaDoc e) {
86                 e.printStackTrace();
87             }
88         }
89
90         if (time == -1)
91             return null;
92         Calendar JavaDoc cal = Calendar.getInstance();
93         cal.setTimeInMillis(time);
94         return cal;
95
96     }
97
98     public static Calendar JavaDoc createCalendarFromString(String JavaDoc str) {
99         if (str == null)
100             throw new IllegalArgumentException JavaDoc("str == null");
101
102         if (str.length() == 0)
103             throw new IllegalArgumentException JavaDoc("str.length() == 0");
104
105         Calendar JavaDoc c = Calendar.getInstance();
106
107         try {
108             Date JavaDoc d = DateFormat.getInstance().parse(str);
109             c.setTime(d);
110         } catch (ParseException JavaDoc e) {
111             e.printStackTrace();
112         }
113
114         return c;
115     }
116
117 }
118
Popular Tags