tags:

views:

211

answers:

1

In my app, I am persisting a CookieStore by writing it out to a file as a long String using SharedPreferences. When reading this String back in, I parse each cookie and its attributes (name, value, domain, path, version, expiry date) from the String using regexps. As I parse these attributes, I reconstruct a CookieStore by making a new BasicClientCookie object for each cookie that is parsed, then add each one to a new CookieStore.

My issue comes up when I am parsing the expiry date as follows:

CookieStore cs = new CookieStore();
SimpleDateFormat sdf = new SimpleDateFormat( "EEE MMM d HH:mm:ss zzz yyyy" );
//name, value, domain, path, expiry, and version are cookie attribute strings that I have parsed
//for(each cookie)
BasicClientCookie bcc = new BasicClientCookie( name, value );
bcc.setDomain(domain);
bcc.setPath(path);
bcc.setVersion(version);
bcc.setExpiryDate( sdf.parse(expiry) );
cs.addCookie(bcc);

This works fine in OS 1.5: the SimpleDateFormat object is able to parse expiry dates with the following format: [expiry: Fri Apr 02 11:23:38 PDT 2010]

However, in OS 2.0 and above, the expiry dates are formatted differently: [expiry: Fri Apr 02 11:28:21 America/Los_Angeles 2010]

The timezone formatting differs in OS 2.0+ so the sdf.parse() method throws: W/System.err(10134): java.text.ParseException: Unparseable date: Fri Apr 02 11:35:34 America/Los_Angeles 2010

Now according to this, I can parse timezones like "America/Los_Angeles" using the "v" format specifier. So I try an sdf object with "EEE MMM d HH:mm:ss vvv yyyy" format string but it does not recognize the "v" format specifier.

E/AndroidRuntime(10194): java.lang.IllegalArgumentException: Unknown pattern character - 'v' E/AndroidRuntime(10194): at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:379)

I am building my project w/ the 1.5 SDK and my Java version is 1.6. I have not been able to find out why the "v" format specifier is not recognized. I am importing java.text.SimpleDateFormat. Is that the wrong one? Does anyone see a better approach to this?

+4  A: 

Why not just persist the date as milliseconds since the epoch, then pass it into new Date(msecs)?

Christopher
do you mean as I am persisting the cookies i can for each cookie :cookie.getExpiryDate().getTime() then persist that out as a long? that seems like a good approach i'll try that out thanks
Exactly. Date parsing has quite an overhead too, so should be faster and with less code :)
Christopher
it worked thanks :)