1 17 18 package org.apache.tomcat.util.buf; 19 20 import java.text.DateFormat ; 21 import java.text.FieldPosition ; 22 import java.text.ParseException ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 25 import java.util.Locale ; 26 import java.util.TimeZone ; 27 28 import org.apache.tomcat.util.res.StringManager; 29 30 40 public class DateTool { 41 42 44 private final static Locale LOCALE_US = Locale.US; 45 46 48 public final static TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT"); 49 50 52 public final static String RFC1123_PATTERN = 53 "EEE, dd MMM yyyy HH:mm:ss z"; 54 55 public final static String rfc1036Pattern = 57 "EEEEEEEEE, dd-MMM-yy HH:mm:ss z"; 58 59 public final static String asctimePattern = 61 "EEE MMM d HH:mm:ss yyyy"; 62 63 65 private final static String OLD_COOKIE_PATTERN = "EEE, dd-MMM-yyyy HH:mm:ss z"; 66 67 69 private final static DateFormat rfc1123Format = 70 new SimpleDateFormat (RFC1123_PATTERN, LOCALE_US); 71 72 75 private final static DateFormat oldCookieFormat = 76 new SimpleDateFormat (OLD_COOKIE_PATTERN, LOCALE_US); 77 78 private final static DateFormat rfc1036Format = 79 new SimpleDateFormat (rfc1036Pattern, LOCALE_US); 80 81 private final static DateFormat asctimeFormat = 82 new SimpleDateFormat (asctimePattern, LOCALE_US); 83 84 static { 85 rfc1123Format.setTimeZone(GMT_ZONE); 86 oldCookieFormat.setTimeZone(GMT_ZONE); 87 rfc1036Format.setTimeZone(GMT_ZONE); 88 asctimeFormat.setTimeZone(GMT_ZONE); 89 } 90 91 private static String rfc1123DS; 92 private static long rfc1123Sec; 93 94 private static StringManager sm = 95 StringManager.getManager("org.apache.tomcat.util.buf.res"); 96 97 static long parseDate( MessageBytes value ) { 99 return parseDate( value.toString()); 100 } 101 102 105 public static String format1123( Date d ) { 106 String dstr=null; 107 synchronized(rfc1123Format) { 108 dstr = format1123(d, rfc1123Format); 109 } 110 return dstr; 111 } 112 113 public static String format1123( Date d,DateFormat df ) { 114 long dt = d.getTime() / 1000; 115 if ((rfc1123DS != null) && (dt == rfc1123Sec)) 116 return rfc1123DS; 117 rfc1123DS = df.format( d ); 118 rfc1123Sec = dt; 119 return rfc1123DS; 120 } 121 122 123 126 public static void formatOldCookie( Date d, StringBuffer sb, 127 FieldPosition fp ) 128 { 129 synchronized(oldCookieFormat) { 130 oldCookieFormat.format( d, sb, fp ); 131 } 132 } 133 134 public static String formatOldCookie( Date d ) 136 { 137 String ocf=null; 138 synchronized(oldCookieFormat) { 139 ocf= oldCookieFormat.format( d ); 140 } 141 return ocf; 142 } 143 144 145 148 public static long parseDate( String dateString ) { 149 DateFormat [] format = {rfc1123Format,rfc1036Format,asctimeFormat}; 150 return parseDate(dateString,format); 151 } 152 public static long parseDate( String dateString, DateFormat []format ) { 153 Date date=null; 154 for(int i=0; i < format.length; i++) { 155 try { 156 date = format[i].parse(dateString); 157 return date.getTime(); 158 } catch (ParseException e) { } 159 catch (StringIndexOutOfBoundsException e) { } 160 } 161 String msg = sm.getString("httpDate.pe", dateString); 162 throw new IllegalArgumentException (msg); 163 } 164 165 } 166 | Popular Tags |