0

I am using JodaTime 2.9.9:

implementation 'joda-time:joda-time:2.9.9'

I am parsing the same Date/Time string in in a Local UnitTest (test) and on an Instrumented UnitTest (androidTest).

The parsing code is:

    String testString = "1/25/2018 5:03:12 PM";
    System.out.println("FOO - str: '" + testString + "' length: " + testString.length());
    DateTime res = DateTime.parse(testString, DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ss a"));
    System.out.println("FOO - prs: " + res.toString());
    assertEquals("Month: ", 1, res.getMonthOfYear());
  • On Local UnitTest (test) - test passes:

FOO - str: '1/25/2018 5:03:12 PM' length: 20 FOO - prs: 2018-01-25T17:03:12.000Z

  • On Instrumented UnitTest (androidTest) - test fails:

java.lang.IllegalArgumentException: Invalid format: "1/25/2018 5:03:12 PM" is malformed at "PM" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)

Why?

RumburaK
  • 1,985
  • 1
  • 21
  • 30
  • 1
    The error message implies that it is treating the `testString` value as the pattern, rather than data to be parsed. It is likely there is a library version mismatch, or an API has changed. – Bob Dalgleish Jan 25 '18 at 18:36
  • No, I have checked it in the debugger. The pattern is really an object ```DateTimeFormatter```. I have also read the docs and tried similar ways of calling the parser, also used ```LocalDateTime```. – RumburaK Jan 26 '18 at 15:19
  • The string parsed is ```testString```. To make it even more bizarre, it happens on a device with Marshmallow 6.0.1 (Moto G 3rd gen). It doesn't happen on Lollipop or Oreo, neither on a Marshmallow emulator! – RumburaK Jan 26 '18 at 15:29
  • 1
    Different devices can have different text resources. This is relevant in case of JodaTime because that library has no own independent resources but just delegate to the Android OS. Either you use any kind of string preprocessing before parsing, or use another library which has its own resources (for example my lib [Time4A](https://github.com/MenoData/Time4A) which is also capable of parsing "PM" even if the associated resource for a given locale has another format). – Meno Hochschild Jan 26 '18 at 16:52
  • It's possible. The debugger doesn't work well inside the lib so I can't quite verify that at the moment. I hope someone will provide an answer on how to keep joda-time and use it properly on Android. – RumburaK Jan 28 '18 at 10:47

0 Answers0