KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > date > DateFormatter


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.webdav.date;
20
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24
25 /**
26  * Handles either a SimpleISO8601DateFormat or SimpleRFC1123DateFormat which are
27  * used in the webdav API.
28  *
29  * @author MATT TREANOR
30  * @version $Revision: 1.1 $
31  *
32  */

33 public class DateFormatter {
34
35     private static Date JavaDoc m_Date = null;
36
37     private static String JavaDoc m_sRFCFormat = "EEE, dd MMM yyyy HH:mm:ss";
38
39     private static String JavaDoc m_sISOFormat = "yyyy-MM-dd'T'HH:mm:ss";
40
41     /**
42      *
43      */

44     public DateFormatter() {
45     }
46
47     /**
48      * @param sDate
49      * @param sFormat
50      * @return
51      * @throws ParseException
52      */

53     public static String JavaDoc format(String JavaDoc sDate, String JavaDoc sFormat)
54             throws ParseException JavaDoc {
55         SimpleDateFormat JavaDoc df;
56         //SimpleRFC1123DateFormat
57
df = new SimpleDateFormat JavaDoc(m_sRFCFormat);
58         try {
59             m_Date = df.parse(sDate);
60         } catch (ParseException JavaDoc e) {
61             // try again
62
}
63         if (m_Date == null) {
64             //SimpleISO8601DateFormat
65
df = new SimpleDateFormat JavaDoc(m_sISOFormat);
66             try {
67                 m_Date = df.parse(sDate);
68             } catch (ParseException JavaDoc e1) {
69                 throw new ParseException JavaDoc(
70                         "Date is neither SimpleRFC1123DateFormat"
71                                 + " nor a SimpleISO8601DateFormat", 0);
72             }
73         }
74         df = new SimpleDateFormat JavaDoc(sFormat);
75         return df.format(m_Date);
76     }
77 }
Popular Tags