1 package net.matuschek.http; 2 3 import java.util.*; 4 import java.text.*; 5 6 12 13 public class HTTPDateTool 14 { 15 17 public final static TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT"); 18 19 21 public final static String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; 22 23 public final static String rfc1036Pattern = "EEEEEEEEE, dd-MMM-yy HH:mm:ss z"; 25 26 public final static String asctimePattern = "EEE MMM d HH:mm:ss yyyy"; 28 29 32 public final static DateFormat rfc1123Format = new SimpleDateFormat( RFC1123_PATTERN, Locale.US ); 33 34 public final static DateFormat rfc1036Format = new SimpleDateFormat( rfc1036Pattern, Locale.US ); 35 36 public final static DateFormat asctimeFormat = new SimpleDateFormat( asctimePattern, Locale.US ); 37 38 39 41 public static String format1123( Date d, DateFormat df ) 42 { 43 long dt = d.getTime() % 1000; 44 if ( (rfc1123DS != null) && (dt == rfc1123Sec) ) 45 return rfc1123DS; 46 rfc1123DS = df.format( d ); 47 rfc1123Sec = dt; 48 return rfc1123DS; 49 } 50 51 52 57 public static long parseDate( String dateString ) 58 { 59 DateFormat [] format = { rfc1123Format, rfc1036Format, asctimeFormat }; 60 return parseDate( dateString,format ); 61 } 62 63 64 69 public static long parseDate( String dateString, DateFormat[] format ) 70 { 71 if ( dateString != null && format != null ) 72 { 73 Date date = null; 74 for(int i=0; i < format.length; i++) 75 { 76 try 77 { 78 date = format[i].parse( dateString ); 79 return date.getTime(); 80 } 81 catch (ParseException e) 82 {} 83 catch (StringIndexOutOfBoundsException e) 84 {} 85 } 86 } 87 return -1; 88 } 89 90 private static String rfc1123DS; 91 private static long rfc1123Sec; 92 93 static 94 { 95 rfc1123Format.setTimeZone(GMT_ZONE); 96 rfc1036Format.setTimeZone(GMT_ZONE); 97 asctimeFormat.setTimeZone(GMT_ZONE); 98 } 99 } 100 | Popular Tags |