01: /*
02: * The contents of this file are subject to the
03: * Mozilla Public License Version 1.1 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
06: *
07: * Software distributed under the License is distributed on an "AS IS"
08: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
09: * 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;
22: import java.text.SimpleDateFormat;
23: import java.util.Date;
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 m_Date = null;
36:
37: private static String m_sRFCFormat = "EEE, dd MMM yyyy HH:mm:ss";
38:
39: private static String 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 format(String sDate, String sFormat)
54: throws ParseException {
55: SimpleDateFormat df;
56: //SimpleRFC1123DateFormat
57: df = new SimpleDateFormat(m_sRFCFormat);
58: try {
59: m_Date = df.parse(sDate);
60: } catch (ParseException e) {
61: // try again
62: }
63: if (m_Date == null) {
64: //SimpleISO8601DateFormat
65: df = new SimpleDateFormat(m_sISOFormat);
66: try {
67: m_Date = df.parse(sDate);
68: } catch (ParseException e1) {
69: throw new ParseException(
70: "Date is neither SimpleRFC1123DateFormat"
71: + " nor a SimpleISO8601DateFormat", 0);
72: }
73: }
74: df = new SimpleDateFormat(sFormat);
75: return df.format(m_Date);
76: }
77: }
|