1 31 32 package org.apache.commons.httpclient.util; 33 34 import java.text.ParseException ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Date ; 37 import java.util.Locale ; 38 import java.util.TimeZone ; 39 40 48 public class DateParser { 49 50 53 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; 54 55 58 public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz"; 59 60 64 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; 65 66 67 private static final String [] DATE_PATTERNS = { 68 PATTERN_RFC1123, 69 PATTERN_RFC1036, 70 PATTERN_ASCTIME, 71 "EEE, dd-MMM-yyyy HH:mm:ss z", 72 "EEE, dd-MMM-yyyy HH-mm-ss z", 73 "EEE, dd MMM yy HH:mm:ss z", 74 "EEE dd-MMM-yyyy HH:mm:ss z", 75 "EEE dd MMM yyyy HH:mm:ss z", 76 "EEE dd-MMM-yyyy HH-mm-ss z", 77 "EEE dd-MMM-yy HH:mm:ss z", 78 "EEE dd MMM yy HH:mm:ss z", 79 "EEE,dd-MMM-yy HH:mm:ss z", 80 "EEE,dd-MMM-yyyy HH:mm:ss z", 81 "EEE, dd-MM-yyyy HH:mm:ss z", 82 }; 83 84 94 public static Date parseDate(String dateValue) throws DateParseException { 95 return parseDate(dateValue, DATE_PATTERNS); 96 } 97 98 108 private static Date parseDate( 109 String dateValue, 110 String [] dateFormats 111 ) throws DateParseException { 112 113 if (dateValue == null) { 114 throw new IllegalArgumentException ("dateValue is null"); 115 } 116 117 SimpleDateFormat dateParser = null; 118 119 for (int i = 0; i < dateFormats.length; i++) { 120 if (dateParser == null) { 121 dateParser = new SimpleDateFormat (dateFormats[i], Locale.US); 122 dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); 123 } else { 124 dateParser.applyPattern(dateFormats[i]); 125 } 126 try { 127 return dateParser.parse(dateValue); 128 } catch (ParseException pe) { 129 } 131 } 132 133 throw new DateParseException("Unable to parse the date " + dateValue); 135 } 136 137 138 private DateParser() { } 139 140 } 141 | Popular Tags |