0

I am parsing with DateFormatter "Thu Jun 28 14:25:00 GMT+03:00 2018", and it correctly outputs Jun 28, 2018 2:25:00 pm. However when the user switches to Spanish locale the formatter outputs jun. 28, 2018 2:25:00 p. m. Is this normal?

here is how I'm parsing the date

    DateFormatter dateFormatter = new DateFormatter();
    dateFormatter.setDateFormatPattern("MMM dd, yyyy hh:mm:ss a");
    mCalendar = new GregorianCalendar(mTimeZone);
    mCalendar.setTime(date);
    mSimpleDateFormat.applyPattern(pattern);
    mSimpleDateFormat.setTimeZone(mTimeZone);

    mSimpleDateFormat.format(mCalendar.getTime());
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Libathos
  • 3,340
  • 5
  • 36
  • 61
  • 1
    Apparently this is normal, I just googled for Spanish representation of AM/PM and found a bunch of articles about this. I think this is expected.. Interesting, indeed :) – Gennadii Saprykin Jun 29 '18 at 08:21
  • @GennadiiSaprykin hello thanks for your comment, Could you post it as an answer mentioning at least an article so that I can mark it as accepted just for anyone that stumble upon this post in the future? – Libathos Jun 29 '18 at 09:01
  • I don’t speak Spanish, but is AM and PM used in Spanish at all? Also what result would you have desired? – Ole V.V. Jun 29 '18 at 09:15

3 Answers3

3

It is the correct representation. For example, compare the Spanish AM/PM Strings and the Italian AM/PM Strings. You'll see that Spanish is p.m and Italian is PM.

Although you may want to refer to this Quora answer which states that Spanish times are always presented in a 24-hour format.

vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • The Quora answer referred is clear in establishing that it applies only for Spain. In Latin America, hours are presented in 12-hour format. I don't know if there is a rule that establishes the way in which am/pm has to be written, but let me ask RAE – Daniel Lema Dec 01 '21 at 17:06
2

Γεια σου. I believe that vguzzi’s answer is correct. I would like to add two messages:

  • Avoid the outdated classes GregorianCalendar, Date, TimeZone and the notoriously troublesome SimpleDateFormat. Instead use java.time, the modern Java date and time API.
  • Use the built-in localized formats rather than rolling your own to match the expectations of a local audience much better.

Code example:

    DateTimeFormatter originalFormatter
            = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
    DateTimeFormatter userFormatter 
            = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    String originalDateTimeString = "Thu Jun 28 14:25:00 GMT+03:00 2018";
    ZonedDateTime dateTime = ZonedDateTime.parse(originalDateTimeString, originalFormatter);
    String formattedDateTime = dateTime.format(userFormatter);
    System.out.println(formattedDateTime);

When run in US locale this prints a result that resembles yours:

Jun 28, 2018, 2:25:00 PM

Already British locale is a bit different:

28 Jun 2018, 14:25:00

There’s English and English. And Java knows that Spanish doesn’t use AM and PM either:

28 jun. 2018 14:25:00

It was a surprise to me to learn that Greek does:

28 Ιουν 2018, 2:25:00 μ.μ.

The output examples are from the code running on my Java 9. The locale data on Android may differ giving slightly different output in some cases, but still well suited to the different local audiences.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Yes, that's normal. I do some localization testing. :)

If it's Spanish (Spain) / ES_ES - then it would be a 24 hour format.

If it's Spanish (USA) / ES_US - then it would be a 12 hour format. There would need to be a space between the a. m. and p. m.