tags:

views:

159

answers:

3

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 void 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"
A: 

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

Skrud
+1  A: 

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?

A: 

Hi, i got same problem too, as i debug problem belong method to convert form Date to string, i have got an solution here: http://stackoverflow.com/questions/3905545/android-load-timezone-too-long-loaded-time-zone-names-for-en-us

And performance was increase 10 time :)

nguyendat