Duplicate :
|
FULL PRODUCT VERSION : java version "1.6.0_02" Java(TM) SE Runtime Environment (build 1.6.0_02-b06) Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing) ADDITIONAL OS VERSION INFORMATION : Microsoft Windows XP [Version 5.1.2600] A DESCRIPTION OF THE PROBLEM : When Locale is Locale.JAPAN, HttpCookie can't parse Expires and always set MaxAge to 0. I show the detail below: 1. HTTP server sends cookie like: Set-Cookie: id=1234; Expires=Sat, 01-Sep-2007 05:03:41 GMT; 2. HttpCookie.parse method is called, and then expiryDate2DeltaSecond method is called for parsing Expires attribute. private final static String NETSCAPE_COOKIE_DATE_FORMAT = "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'"; private long expiryDate2DeltaSeconds(String dateString) { SimpleDateFormat df = new SimpleDateFormat(NETSCAPE_COOKIE_DATE_FORMAT); df.setTimeZone(TimeZone.getTimeZone("GMT")); try { Date date = df.parse(dateString); return (date.getTime() - whenCreated) / 1000; } catch (Exception e) { e.printStackTrace(); return 0; } } 3. When Locale is Locale.JAPAN, EEE of SimpleDateFormat is indicated by Kanji character and MMM is indicated by number like: [SEE char.txt attachment], 31-8-2007 05:03:41 GMT Therefore, df.parse throws ParseException. java.text.ParseException: Unparseable date: "Sat, 01-Sep-2007 05:03:41 GMT" at java.text.DateFormat.parse(DateFormat.java:337) at java.net.HttpCookie.expiryDate2DeltaSeconds(HttpCookie.java:1049) 4. Exception is caught by catch clase, and return value is 0. i.e. MaxAge is 0. For fixing bug, instantiate SimpleDateFormat with Locale explicitly. SimpleDateFormat df = new SimpleDateFormat(NETSCAPE_COOKIE_DATE_FORMAT, Locale.US); REPRODUCIBILITY : This bug can be reproduced always.