Cookie的分析问题瓦特/日期的Andr​​oid日期、问题、Cookie、Andr

2023-09-06 06:35:25 作者:一半回忆、一半继续

在我的应用程序,我写出来给一个文件使用共享preferences一长串坚持一个的CookieStore。当回读这个字符串,我分析每个Cookie,并使用正则表达式字符串的属性(名称,价值,域名,路径,版本,到期日)。当我分析这些属性,我通过为被解析每个Cookie新BasicClientCookie对象重建的CookieStore,那么每一个添加到新的CookieStore。

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);

这在OS 1.5正常工作:SimpleDateFormat的目标是能够解析到期以下格式日期:有效期:周五4月2日11时23分38秒PDT 2010]

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]

不过,在OS 2.0及以上的,期满日期的格式不同:[届满:周五4月2日11点28分21秒美洲/洛杉矶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]

时区不同的格式在OS 2.0+所以sdf.parse()方法抛出:W / System.err的(10134):java.text.ParseException:无法解析日期:周五4月2日11时35分34秒美洲/洛杉矶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

现在根据这,我可以分析象美国时区/洛杉矶用v格式说明。于是我尝试用一​​个SDF对象EEE年MMM d HH:MM:SS VVV YYYY格式字符串,但它不承认V格式说明

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异常:未知模式字符 - VE / AndroidRuntime(10194):在java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:379)

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

我建立我的项目瓦特/ 1.5 SDK和我的Java版本是1.6。我一直没能找出为什么V格式说明不被认可。我进口java.text.SimpleDateFormat中。那是错误的吗?有谁看到一个更好的办法来呢?

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?

推荐答案

为什么不坚持的日期作为毫秒自时代,然后将它传递到新的日期(毫秒)

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