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 SimpleISO8601DateFormat extends DateFormat { 37 38 static final long serialVersionUID = 4774881970558875024L; 41 42 static final int currentSerialVersion = 1; 46 private int serialVersionOnStream = currentSerialVersion; 47 54 public SimpleISO8601DateFormat() { 55 initialize(); 56 } 57 60 public Object clone() { 61 SimpleISO8601DateFormat other = (SimpleISO8601DateFormat) super.clone(); 62 return other; 64 } 65 68 public boolean equals(Object obj) { 69 if (!super.equals(obj)) 70 return false; 72 75 return true; 76 } 77 88 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) { 89 java.text.SimpleDateFormat df = new java.text.SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss"); 90 df.setTimeZone(TimeZone.getTimeZone("GMT")); 91 df.format(date, toAppendTo, pos); 92 Calendar calendar = Calendar.getInstance(); 93 calendar.setTime(date); 94 int dstoff = calendar.get(Calendar.DST_OFFSET); 95 int tzoff = calendar.get(Calendar.ZONE_OFFSET); 96 97 tzoff += dstoff; 98 tzoff /= (1000 * 60); 99 if (tzoff != 0) { 100 if (tzoff < 0) { 101 toAppendTo.append("-"); 102 tzoff *= -1; 103 } else { 104 toAppendTo.append("+"); 105 } 106 int hr = tzoff / 60; 107 if (hr < 10) 108 toAppendTo.append("0"); 109 toAppendTo.append("" + hr + ":"); 110 int mn = tzoff % 60; 111 if (mn < 10) 112 toAppendTo.append("0"); 113 toAppendTo.append(mn); 114 } else { 115 toAppendTo.append("Z"); 116 } 117 181 return toAppendTo; 182 } 183 184 185 private void initialize() { 186 TimeZone tz = TimeZone.getTimeZone("UDT"); 191 calendar = Calendar.getInstance(tz); 192 NumberFormat numberFormat = NumberFormat.getInstance(); 194 201 setNumberFormat(numberFormat); 202 205 } 206 210 public Date parse(String text, java.text.ParsePosition pos) { 211 int start = pos.getIndex(); 212 Calendar calendar = getCalendar(); 213 calendar.clear(); 215 int year = Integer.parseInt(text.substring(start, start + 4)); 216 int month = Integer.parseInt(text.substring(start + 5, start + 7)); 217 int day = Integer.parseInt(text.substring(start + 8, start + 10)); 218 int hour = Integer.parseInt(text.substring(start + 11, start + 13)); 219 int minute = Integer.parseInt(text.substring(start + 14, start + 16)); 220 int second = Integer.parseInt(text.substring(start + 17, start + 19)); 221 if (text.substring(start + 19, start + 20).equals("Z")) { 222 pos.setIndex(start + 20); 224 } else { 225 int offhour = Integer.parseInt(text.substring(start + 20, start + 22)); 226 int offmin = Integer.parseInt(text.substring(start + 23, start + 25)); 227 int sum = (offhour * 60 + offmin) * 60 * 1000; if (text.substring(start + 19, start + 20).equals("-")) 229 sum *= -1; calendar.set(Calendar.ZONE_OFFSET, sum); pos.setIndex(start + 25); 233 } 234 235 237 calendar.set(year, month - 1, day, hour, minute, second); 238 Date retval = calendar.getTime(); 239 return retval; 241 } 242 245 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 246 stream.defaultReadObject(); 247 257 serialVersionOnStream = currentSerialVersion; 258 } 259 } 260 | Popular Tags |