2

I am struggling converting date from "hh:mm a" into "HH:mm" in android. While I don't get any errors on simple java application, I get an error on android. Here is the code:

String time = "02:00 PM";
String formattedTime = "";
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
String parseFormats[] = new String[]{"HH:mm", "HHmm", "hh:mm a", "hh a"};

for (String parseFormat : parseFormats) {
    SimpleDateFormat formatting = new SimpleDateFormat(parseFormat);
    try {
        Date date = formatting.parse(time);
        formattedTime = displayFormat.format(date);
        System.out.println(formattedTime);
    } catch (ParseException e) {
        System.out.println(parseFormat);
        e.printStackTrace();
    }
}

in case of Java I get as expected:

02:00
HHmm
java.text.ParseException: Unparseable date: "02:00 PM"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at HelloWorld.main(HelloWorld.java:31)
14:00
hh a
java.text.ParseException: Unparseable date: "02:00 PM"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at HelloWorld.main(HelloWorld.java:31)

in android application the same code returns exception for "hh:mm a" as well:

I/System.out: hh:mm a
W/System.err: java.text.ParseException: Unparseable date: "02:00 PM"

Imports are the same:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

In case of Java app, it succeeds for "HH:mm" and "hh:mm a". In case of Android, it only succeeds for "HH:mm".

Javanshir
  • 772
  • 2
  • 7
  • 20
  • As per the code it is failing at the first parse array elements . It assumes u are passing the date in hh:mm format but the actual date is in hh:mm a format – user8271644 Jul 25 '17 at 15:03
  • well, it fails the first, then continues to the second. The point is: it fails for "hh:mm a" as well. I will update the question to be more specific – Javanshir Jul 25 '17 at 15:04
  • 2
    Might be related to the default locale. `AM/PM` might be seen as an error for a country which uses the 24h system. Can you try to provide a Locale somewhere when you instantiate the `DateFormat`? – Arnaud Denoyelle Jul 25 '17 at 15:12
  • It works for "h:mm a" in android – Ayush Khare Jul 25 '17 at 15:14
  • Ditto^. Works for `h:mm a` here as well. – fluffyBatman Jul 25 '17 at 15:17
  • 1
    @ArnaudDenoyelle, yes, that is the problem. I added Locale.France and it worked. However, in default or Locale.Germany it doesn't work – Javanshir Jul 25 '17 at 15:24
  • How hard did you search before asking? This question has been asked and answered a couple of times before. Always search before asking for the benefit if yourself (you will get the answer faster) and others (who will find all the information pertaining to the problem in one place, not scattered among many similar questions). – Ole V.V. Jul 25 '17 at 17:34

1 Answers1

0

Found the problem thanks to @ArnaudDenoyelle. I checked SimpleDateFormat class, turns out 1 valued constructor calls 2 valued with defaul Locale:

public SimpleDateFormat(String pattern)
    {
        this(pattern, Locale.getDefault(Locale.Category.FORMAT));
    }

AM/PM is seen as an error for my country, as it uses 24 hour system. As my phone and simple java application return different default Locale values, I was getting different results.

While it is not a solution, I used Locale.France to avoid the problem:

SimpleDateFormat formatting = new SimpleDateFormat(parseFormat, Locale.FRANCE);
Javanshir
  • 772
  • 2
  • 7
  • 20