KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > SimpleRFC1123DateFormat


1 package com.ibm.webdav;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17
18 import java.io.*;
19 import java.text.*;
20 import java.util.*;
21
22 /**
23  * <code>SimpleRFC1123DateFormat</code> is a concrete class for formatting and
24  * parsing RFC 1123 format dates. It allows for formatting
25  * (date -> text), parsing (text -> date), and normalization.
26  *
27  * @see java.util.Calendar
28  * @see java.util.GregorianCalendar
29  * @see java.util.TimeZone
30  * @see DateFormat
31  * @see DateFormatSymbols
32  * @see DecimalFormat
33  * @version 1.31 04/22/98
34  * @author Mark Davis, Chen-Lieh Huang, Alan Liu, Jason Crawford
35  */

36 public class SimpleRFC1123DateFormat extends DateFormat {
37
38     // the official serial version ID which says cryptically
39
// which version we're compatible with
40
static final long serialVersionUID = 4774881970558875024L;
41
42     // the internal serial version which says which version was written
43
// - 0 (default) for version up to JDK 1.1.3
44
// - 1 for version from JDK 1.1.4, which includes a new field
45
static final int currentSerialVersion = 1;
46     private int serialVersionOnStream = currentSerialVersion;
47
48     static Hashtable htMonths = new Hashtable();
49     static {
50         htMonths.put("Jan", new Integer JavaDoc(1));
51         htMonths.put("Feb", new Integer JavaDoc(2));
52         htMonths.put("Mar", new Integer JavaDoc(3));
53         htMonths.put("Apr", new Integer JavaDoc(4));
54         htMonths.put("May", new Integer JavaDoc(5));
55         htMonths.put("Jun", new Integer JavaDoc(6));
56         htMonths.put("Jul", new Integer JavaDoc(7));
57         htMonths.put("Aug", new Integer JavaDoc(8));
58         htMonths.put("Sep", new Integer JavaDoc(9));
59         htMonths.put("Oct", new Integer JavaDoc(10));
60         htMonths.put("Nov", new Integer JavaDoc(11));
61         htMonths.put("Dec", new Integer JavaDoc(12));
62     };
63     
64 /**
65  * Construct a SimpleDateFormat using the default pattern for the default
66  * locale. <b>Note:</b> Not all locales support SimpleDateFormat; for full
67  * generality, use the factory methods in the DateFormat class.
68  *
69  * @see java.text.DateFormat
70  */

71 public SimpleRFC1123DateFormat() {
72     initialize();
73 }
74 /**
75  * Overrides Cloneable
76  */

77 public Object JavaDoc clone() {
78     SimpleRFC1123DateFormat other = (SimpleRFC1123DateFormat) super.clone();
79 // other.formatData = (DateFormatSymbols) formatData.clone();
80
return other;
81 }
82 /**
83  * Override equals.
84  */

85 public boolean equals(Object JavaDoc obj) {
86     if (!super.equals(obj))
87         return false; // super does class check
88

89     // todo: I think we are supposed to check if they are equivalent, but for now a class check will do. In fact I think
90
// just being the same class is adequate.
91

92     return true;
93 }
94 /**
95  * Overrides DateFormat
96  * <p>Formats a date or time, which is the standard millis
97  * since January 1, 1970, 00:00:00 GMT.
98  * @param date the date-time value to be formatted into a date-time string.
99  * @param toAppendTo where the new date-time text is to be appended.
100  * @param pos the formatting position. On input: an alignment field,
101  * if desired. On output: the offsets of the alignment field.
102  * @return the formatted date-time string.
103  * @see java.util.DateFormat
104  */

105 public StringBuffer JavaDoc format(Date date, StringBuffer JavaDoc toAppendTo, FieldPosition pos) {
106     java.text.SimpleDateFormat JavaDoc df = new java.text.SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
107     df.setTimeZone(TimeZone.getTimeZone("GMT"));
108     df.format(date, toAppendTo, pos);
109     return toAppendTo;
110 }
111 /* Initialize calendar and numberFormat fields */
112
113 private void initialize() {
114     // The format object must be constructed using the symbols for this zone.
115
// However, the calendar should use the current default TimeZone.
116
// If this is not contained in the locale zone strings, then the zone
117
// will be formatted using generic GMT+/-H:MM nomenclature.
118
TimeZone tz = TimeZone.getTimeZone("UDT");
119     calendar = Calendar.getInstance(tz);
120     // numberFormat isn't used by us, but we inherit behavior that we have to respect.
121
NumberFormat numberFormat = NumberFormat.getInstance();
122     /*
123     numberFormat.setGroupingUsed(false);
124     if (numberFormat instanceof DecimalFormat)
125     ((DecimalFormat)numberFormat).setDecimalSeparatorAlwaysShown(false);
126     numberFormat.setParseIntegerOnly(true); // So that dd.mm.yy can be parsed
127     numberFormat.setMinimumFractionDigits(0); // To prevent "Jan 1.00, 1997.00"
128     */

129     setNumberFormat(numberFormat);
130     /*
131     initializeDefaultCentury();
132     */

133 }
134 public static void main(String JavaDoc args[]) {
135     SimpleRFC1123DateFormat sr = new SimpleRFC1123DateFormat();
136     java.text.ParsePosition JavaDoc pp = new java.text.ParsePosition JavaDoc(0);
137     Date dt = null;
138     String JavaDoc arg1 = null;
139     
140     arg1 = "Sunday, 05-Jan-2000 08:42:03 GMT";
141     dt = sr.parse( arg1, new java.text.ParsePosition JavaDoc(0) ); System.out.println( arg1+" gives "+dt );
142     arg1 = "Sun, 05-Jan-98 08:42:03 GMT";
143     dt = sr.parse( arg1, new java.text.ParsePosition JavaDoc(0) ); System.out.println( arg1+" gives "+dt );
144     StringBuffer JavaDoc sbb = new StringBuffer JavaDoc();
145     sr.format( dt, sbb, new java.text.FieldPosition JavaDoc(0) );
146     System.out.println( sbb );
147     System.exit(0);
148 }
149 /**
150  * Overrides DateFormat
151  * @see java.util.DateFormat
152  */

153 public Date parse(String JavaDoc text, java.text.ParsePosition JavaDoc pos) {
154     int start0 = pos.getIndex();
155     Calendar calendar = getCalendar();
156     calendar.clear(); // Clears all the time fields
157

158     int start = text.indexOf( ',', start0);
159     if (-1 == start) return null;
160
161     start += 2;
162     int day = Integer.parseInt(text.substring(start + 0, start + 2));
163     String JavaDoc stMonth = text.substring(start + 3, start + 6);
164     Integer JavaDoc itMonth = (Integer JavaDoc)htMonths.get(stMonth);
165     if (itMonth==null) return null;
166     int month = itMonth.intValue();
167     
168     char pivotChar = text.charAt( start+9);
169     int year;
170     if ( (pivotChar < '0') || (pivotChar > '9')) {
171         // apparently a two digit year
172
year = Integer.parseInt(text.substring(start+7, start+9));
173         if (year<40) { year += 2000; } else { year += 1900; };
174         start -= 2;
175     } else {
176         year = Integer.parseInt(text.substring(start+7, start+11));
177     }
178     int hour = Integer.parseInt(text.substring(start + 12, start + 14));
179     int minute = Integer.parseInt(text.substring(start + 15, start + 17));
180     int second = Integer.parseInt(text.substring(start + 18, start + 20));
181     String JavaDoc stGMT = text.substring( start + 21, start + 24);
182     if (!stGMT.equals("GMT")) {
183         return null;
184     } else {
185         calendar.set(Calendar.ZONE_OFFSET, /*GMT:*/0 ); // NYC is ZONE_OFFSET=5 or 4
186
//calendar.set( Calendar.DST_OFFSET, sum );
187
pos.setIndex(start + 29);
188     }
189
190
191     calendar.set(year, month - 1, day, hour, minute, second);
192     Date retval = calendar.getTime();
193     //System.out.println( " "+ text + "******" + retval );
194
return retval;
195 }
196 /**
197  * Override readObject.
198  */

199 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException JavaDoc {
200     stream.defaultReadObject();
201     /*
202      if (serialVersionOnStream < 1) {
203      // didn't have defaultCenturyStart field
204      initializeDefaultCentury();
205      }
206      else {
207      // fill in dependent transient field
208      parseAmbiguousDatesAsAfter(defaultCenturyStart);
209      }
210     */

211     serialVersionOnStream = currentSerialVersion;
212 }
213 }
214
Popular Tags