1 package com.ibm.webdav; 2 3 17 18 import java.io.*; 19 import java.text.*; 20 import java.util.*; 21 22 36 public class SimpleRFC1123DateFormat extends DateFormat { 37 38 static final long serialVersionUID = 4774881970558875024L; 41 42 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 (1)); 51 htMonths.put("Feb", new Integer (2)); 52 htMonths.put("Mar", new Integer (3)); 53 htMonths.put("Apr", new Integer (4)); 54 htMonths.put("May", new Integer (5)); 55 htMonths.put("Jun", new Integer (6)); 56 htMonths.put("Jul", new Integer (7)); 57 htMonths.put("Aug", new Integer (8)); 58 htMonths.put("Sep", new Integer (9)); 59 htMonths.put("Oct", new Integer (10)); 60 htMonths.put("Nov", new Integer (11)); 61 htMonths.put("Dec", new Integer (12)); 62 }; 63 64 71 public SimpleRFC1123DateFormat() { 72 initialize(); 73 } 74 77 public Object clone() { 78 SimpleRFC1123DateFormat other = (SimpleRFC1123DateFormat) super.clone(); 79 return other; 81 } 82 85 public boolean equals(Object obj) { 86 if (!super.equals(obj)) 87 return false; 89 92 return true; 93 } 94 105 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) { 106 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ("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 112 113 private void initialize() { 114 TimeZone tz = TimeZone.getTimeZone("UDT"); 119 calendar = Calendar.getInstance(tz); 120 NumberFormat numberFormat = NumberFormat.getInstance(); 122 129 setNumberFormat(numberFormat); 130 133 } 134 public static void main(String args[]) { 135 SimpleRFC1123DateFormat sr = new SimpleRFC1123DateFormat(); 136 java.text.ParsePosition pp = new java.text.ParsePosition (0); 137 Date dt = null; 138 String arg1 = null; 139 140 arg1 = "Sunday, 05-Jan-2000 08:42:03 GMT"; 141 dt = sr.parse( arg1, new java.text.ParsePosition (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 (0) ); System.out.println( arg1+" gives "+dt ); 144 StringBuffer sbb = new StringBuffer (); 145 sr.format( dt, sbb, new java.text.FieldPosition (0) ); 146 System.out.println( sbb ); 147 System.exit(0); 148 } 149 153 public Date parse(String text, java.text.ParsePosition pos) { 154 int start0 = pos.getIndex(); 155 Calendar calendar = getCalendar(); 156 calendar.clear(); 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 stMonth = text.substring(start + 3, start + 6); 164 Integer itMonth = (Integer )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 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 stGMT = text.substring( start + 21, start + 24); 182 if (!stGMT.equals("GMT")) { 183 return null; 184 } else { 185 calendar.set(Calendar.ZONE_OFFSET, 0 ); pos.setIndex(start + 29); 188 } 189 190 191 calendar.set(year, month - 1, day, hour, minute, second); 192 Date retval = calendar.getTime(); 193 return retval; 195 } 196 199 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 200 stream.defaultReadObject(); 201 211 serialVersionOnStream = currentSerialVersion; 212 } 213 } 214 | Popular Tags |