1

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").parse("UTC")

This throws a parse exception. I am using a nexus one device, android 2.2

user508047
  • 49
  • 1
  • 6
  • Your code ran OK for me compiled against the `Android 1.5` jar. What imports are you using for the above classes? Are you running on a device or an emulator? – dave.c Feb 16 '11 at 12:54
  • Running on a nexus one device. The imports are as you would expect – user508047 Feb 18 '11 at 09:33
  • In fact can make the test even simpler. new SimpleDateFormat("z").parse("UTC") UTC does not seem to be supported which is very surprising ...throws an Exception in Andriod 2.2 but not in standard sun JRE – user508047 Feb 18 '11 at 09:34
  • @user508047 Please can you just list your imports for the classes you have listed in your code sample. There are more than one class with the same name that you could be using. – dave.c Feb 18 '11 at 10:35
  • I only know of one SimpleDateFormat? DateFormat and SimpleDateFormat both imported from package java.text – user508047 Feb 21 '11 at 17:51

4 Answers4

2

Use this instead

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
skynet
  • 9,898
  • 5
  • 43
  • 52
Pasha
  • 2,407
  • 18
  • 25
1

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
user508047
  • 49
  • 1
  • 6
0

Try specifying the locale like this:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z", Locale.ENGLISH);
ldx
  • 3,984
  • 23
  • 28
  • if I need to user locale istanbul , how can I do that because locale.turkey or istanbul doesnot exist. soreally I need it? because I write the dateformat template string – CompEng Mar 31 '14 at 07:07
0

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();
            }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
  • sorry, don't see how this helps? – user508047 Feb 18 '11 at 09:33
  • dear you can move on to use this code by changing the date format as per your code – Rohit Mandiwal Feb 18 '11 at 10:25
  • If you got cast exception then you must have used the wrong imports. It makes no difference whether I set a Calendar object with a timezone. I receive strings of the form "2011-02-16 11:38:03.328 UTC" which must parse. This fails on Android 2.2. As I have added to my orignal question the following one liner fails in Android but not on a sun jvm new SimpleDateFormat("z").parse("UTC") I have a workaround now – user508047 Feb 21 '11 at 18:10