Android SimpleDateFormat fails to parse datetime (works fine on sun 1.6 jre)
Any ideas why the followng fails on Android 2.2...
java.text.ParseException: Unparseable date: 2011-02-16 11:38:03.328 UTC
...while it works fine on a sun JRE 1.6 ?
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
try
{
Date date = dateFormat.parse("2011-02-16 11:38:03.328 UTC");
}
catch (ParseException e)
{
e.printStackTrace();
}
As mentioned ion Comment, can make the test even simpler:
new SimpleDateFormat("z").pars开发者_如何转开发e("UTC")
This throws a parse exception. I am using a nexus one device, android 2.2
Use this instead
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
I have a workaround now:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S 'UTC'");
try
{
Date date = dateFormat.parse("2011-02-16 11:38:03.328 UTC");
}
catch (ParseException e)
{
e.printStackTrace();
}
This allows me to at least parse the date in Android 2.2. My application has been modified to try "yyyy-MM-dd HH:mm:ss.S 'UTC'" and then try "yyyy-MM-dd HH:mm:ss.S z" if the first parse fails
Try specifying the locale like this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z", Locale.ENGLISH);
I have copied this code and got casting exception...
try this code of mine
java.text.SimpleDateFormat format = new SimpleDateFormat(
"dd-MM-yyyy");
java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0,
"GMT"));
format.setCalendar(cal);
java.util.Date date = null;
try {
date = format.parse(EditProfile.dateOFBirth);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
精彩评论