0

Has anyone encountered DateTimeParseException on specific devices? I've encountered a crash that happens on a Samsung J7, Android 9 whenever this String is parsed 3/30/2023 12:00:00 AM.

The parsing code looks like this:

fun numberOfDaysBetweenToday(
    dateTime: String,
    clock: Clock,
    zoneId: ZoneId,
    dateTimeFormatter: DateTimeFormatter
): Long {
    val now = Instant.now(clock).atZone(zoneId)
    val other = LocalDateTime.parse(dateTime, dateTimeFormatter).atZone(zoneId)
    return ChronoUnit.DAYS.between(now, other)
}

And I use this function like this:

numberOfDaysBetweenToday("3/30/2023 12:00:00 AM", ZoneId.of("Asia/Manila"), DateTimeFormatter.ofPattern("M/dd/yyyy h:mm:ss a"))

It works on the emulator (Pixel 4), and on my latest Samsung device (Samsung A52s), but on this specific actual device, I'm getting DateTimeParseException: Text '3/30/2023 12:00:00 AM' could not be parsed at index 19

Has anyone encountered something like this?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Alvin Dizon
  • 1,855
  • 14
  • 34
  • 1
    Yes, this is commonplace when you forget to specify a locale on your formatter. While `AM` and `PM` are hardly used in other languages than English in practice, they have got other names in other languages (index 19 in your string is where `AM` is). – Ole V.V. Feb 01 '23 at 05:55
  • @OleV.V. sorry for the late reply, that indeed did the trick, thanks! – Alvin Dizon Feb 07 '23 at 05:38

0 Answers0