Thomas Lann
Thomas Lann

Reputation: 1234

Unparseable UTC datetime

I inherited a bit of code to port to Android. A part I'm having an issue with parses a text string and creates a UTC date from it.

When I run the inherited code I get the error java.text.ParseException: Unparseable date: "4/8/2009 06:00.0" (at offset 14) The offset 14 points to the decimal for the milliseconds. I can't figure out why this is incorrect

Exception Error in total

java.text.ParseException: Unparseable date: "4/8/2009 06:00.0" (at offset 14)
at java.text.DateFormat.parse(DateFormat.java:622)
at gbl.util.test.UTC_test.test2(UTC_test.java:32)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

The code in question looks like

String date = "4/8/2009";
String time = "06:00.0";

SimpleDateFormat utcDateTime = new SimpleDateFormat("d/M/yy HH:mm:ss.SSS");
utcDateTime.setTimeZone(TimeZone.getTimeZone("UTC"));   
utcDateTime.parse(date+" "+time);

Any suggestions would be very appreciated.

Upvotes: 0

Views: 169

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

I can't figure out why this is in correct

You have a 4 digit year instead of a yy = two digit year.

More importantly, you have no seconds field and only one digit for a milli-seconds field.

This is what today's date looks like using this format

24/7/13 23:03:17.171

and this is what your input format looks like

4/8/2009 06:00.0

One doesn't look like the other.

Upvotes: 5

Related Questions