1 30 31 package org.apache.commons.httpclient.util; 32 33 import java.text.ParseException ; 34 import java.text.SimpleDateFormat ; 35 import java.util.Arrays ; 36 import java.util.Calendar ; 37 import java.util.Collection ; 38 import java.util.Date ; 39 import java.util.Iterator ; 40 import java.util.Locale ; 41 import java.util.TimeZone ; 42 43 51 public class DateUtil { 52 53 56 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; 57 58 61 public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz"; 62 63 67 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; 68 69 private static final Collection DEFAULT_PATTERNS = Arrays.asList( 70 new String [] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } ); 71 72 private static final Date DEFAULT_TWO_DIGIT_YEAR_START; 73 74 static { 75 Calendar calendar = Calendar.getInstance(); 76 calendar.set(2000, Calendar.JANUARY, 1, 0, 0); 77 DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); 78 } 79 80 private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); 81 82 93 public static Date parseDate(String dateValue) throws DateParseException { 94 return parseDate(dateValue, null, null); 95 } 96 97 107 public static Date parseDate(String dateValue, Collection dateFormats) 108 throws DateParseException { 109 return parseDate(dateValue, dateFormats, null); 110 } 111 112 126 public static Date parseDate( 127 String dateValue, 128 Collection dateFormats, 129 Date startDate 130 ) throws DateParseException { 131 132 if (dateValue == null) { 133 throw new IllegalArgumentException ("dateValue is null"); 134 } 135 if (dateFormats == null) { 136 dateFormats = DEFAULT_PATTERNS; 137 } 138 if (startDate == null) { 139 startDate = DEFAULT_TWO_DIGIT_YEAR_START; 140 } 141 if (dateValue.length() > 1 144 && dateValue.startsWith("'") 145 && dateValue.endsWith("'") 146 ) { 147 dateValue = dateValue.substring (1, dateValue.length() - 1); 148 } 149 150 SimpleDateFormat dateParser = null; 151 Iterator formatIter = dateFormats.iterator(); 152 153 while (formatIter.hasNext()) { 154 String format = (String ) formatIter.next(); 155 if (dateParser == null) { 156 dateParser = new SimpleDateFormat (format, Locale.US); 157 dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); 158 dateParser.set2DigitYearStart(startDate); 159 } else { 160 dateParser.applyPattern(format); 161 } 162 try { 163 return dateParser.parse(dateValue); 164 } catch (ParseException pe) { 165 } 167 } 168 169 throw new DateParseException("Unable to parse the date " + dateValue); 171 } 172 173 181 public static String formatDate(Date date) { 182 return formatDate(date, PATTERN_RFC1123); 183 } 184 185 198 public static String formatDate(Date date, String pattern) { 199 if (date == null) throw new IllegalArgumentException ("date is null"); 200 if (pattern == null) throw new IllegalArgumentException ("pattern is null"); 201 202 SimpleDateFormat formatter = new SimpleDateFormat (pattern, Locale.US); 203 formatter.setTimeZone(GMT); 204 return formatter.format(date); 205 } 206 207 208 private DateUtil() { } 209 210 } 211 | Popular Tags |