0

I am using

DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
String dateAsString = dateFormat.format(gmt);

And getting String 06-06-2017 08:15 a.m. Why I am getting a.m. instated of AM or PM?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3555472
  • 836
  • 3
  • 11
  • 38

2 Answers2

2

The AM/PM/a.m. actually depends on the device. Try the same code on other devices and you might get to see a different result. If you need AM/PM only, then you need to do it manually by replacing the dots and converting it to uppercase.

Suraj Makhija
  • 1,376
  • 8
  • 16
2

It depends on the locale. If you use SimpleDateFormat (which you may not want to do, see below), I recommend you give it an explicit locale. The one you construct uses the device’s default, which explains why you get different results on different devices. If you want that, use new SimpleDateFormat("MM-dd-yyyy hh:mm a", Locale.getDefault()) so the reader knows you have thought about it. To make sure you get AM and PM, use for example new SimpleDateFormat("MM-dd-yyyy hh:mm a", Locale.ENGLISH).

Why would you not want to use SimpleDateFormat? I consider it long outdated since the much better replacement for the Java 1.0 and 1.1 classes came out with Java 8 in 2014. They have also been backported to Android Java 7 in the ThreeTenABP. Get this and write for example:

    LocalDateTime gmt = LocalDateTime.of(2017, Month.JUNE, 6, 8, 15);
    DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm a",
                                                                   Locale.ENGLISH);
    String dateAsString = gmt.format(dateTimeFormat);

The result is

06-06-2017 08:15 AM

To make explicit that the time is in GMT, you may use an OffsetDateTime with offset ZoneOffset.UTC.

Link: How to use ThreeTenABP in Android Project.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    The OP's issue is not only about the locale. Indeed, different Android devices and platforms use different localized data, even for the same `java.util.Locale`, so using Threeten-ABP would not defend against this behaviour. – Meno Hochschild Jun 06 '17 at 09:34
  • Interesting, @MenoHochschild. I did not know that. – Ole V.V. Jun 06 '17 at 09:35
  • Oops, @MenoHochschild, just saw I forgot the locale in the modern code (contrary to my own advice a few lines earlier). Have fixed. Thanks for drawing my attention back to this answer. ThreeTenABP not defending makes sense: I guess (just a guess, though) that one of the points in ThreeTenABP over ThreeTen Backport is it uses the Android device’s settings like locale. – Ole V.V. Jun 06 '17 at 09:44
  • 1
    The only special point about ThreetenABP (versus the standard backport) is just: It is a thin wrapper around the backport and uses its own timezone provider for performance reasons (uses repackaging of the tz-data as assets). Localization is just delegated to the Android platform but no own i18n-data, no care about special Android settings like 12/24-hour-settings etc. – Meno Hochschild Jun 06 '17 at 09:55