开发者

Weird parsing date string error in Android 2.0 emulator

开发者 https://www.devze.com 2022-12-30 09:22 出处:网络
I have a simple test code for testing SimpleDateFormat. This code works well on Eclipse and Android 1.5 emulator, but it failed at Android 2.0 emulator. Does anyone know why? Thanks.

I have a simple test code for testing SimpleDateFormat. This code works well on Eclipse and Android 1.5 emulator, but it failed at Android 2.0 emulator. Does anyone know why? Thanks.

public class TemplateActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public v开发者_StackOverflow中文版oid onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText(R.string.hello);
       setContentView(tv);

       SimpleDateFormat format =
           new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");

       String dateStr = "Mon, 17 May 2010 01:45:41 GMT"; 

       try {
           Date parsed = format.parse(dateStr);
           Log.v("Test", parsed.toString());
       } catch (ParseException pe) {
           Log.v("Test", "ERROR: Cannot parse \"" + dateStr + "\"");
       }

   }
}

Log message:

V/Test(  400): ERROR: Cannot parse "Mon, 17 May 2010 01:45:41 GMT"


In reply to Skrud,

I tried "EEE, dd MMM yyyy HH:mm:ss z", but got the same error message.

I also tried

SimpleDateFormat format =
       new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);

It works. But the system takes a long time to load time zone names for en_US for each call:

I/Resources(  471): Loaded time zone names for en_US in 1904ms.
D/dalvikvm(  471): GC freed 10658 objects / 486232 bytes in 88ms
I/Resources(  471): Loaded time zone names for en_US in 1400ms.
I/Resources(  471): Loaded time zone names for en_US in 1260ms.
D/dalvikvm(  471): GC freed 10615 objects / 491920 bytes in 91ms
I/Resources(  471): Loaded time zone names for en_US in 1360ms.
I/Resources(  471): Loaded time zone names for en_US in 1232ms.
D/dalvikvm(  471): GC freed 10623 objects / 460544 bytes in 91ms
I/Resources(  471): Loaded time zone names for en_US in 1344ms.
I/Resources(  471): Loaded time zone names for en_US in 1228ms.

What should I do?


Your format string should probably be "EEE, dd MMM yyyy HH:mm:ss z" instead.

Take a look at the examples on the SimpleDateFormat's Javadoc page


i got same problem too, as i debug problem belong method to convert form Date to string, i have got an solution here: Android load timezone too long: Loaded time zone names for en_US

And performance was increase 10 time :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号