Dale Julian Dale Julian - 1 year ago 100
Java Question

Android DateFormat for AM/PM differs between devices

I am formatting dates like this:

public static String toFormattedDate(@NonNull Time time, String toFormat) {
mDateFormat = new SimpleDateFormat(toFormat);

Date date = new Date();

return mDateFormat.format(date);

and the format I am using is:

public static final String TIME = "hh:mm a";

But it differs between the two devices that I am using for testing...

Nexus 10:
Nexus 10

Nexus 5X:
Nexus 5X

How can I format it uniformly between devices?


You may either have to use either the 24 hour value to determine what to append so that you can add the format you desire.

public static final String TIME = "hh:mm";

and then

String ampm = Integer.parseInt(time.valueOf("hh")) >= 12 ? "PM" : "AM";
return mDateFormat.format(date)+" "+ampm;

Or if you feel lazy you can just do without changing the value of TIME:

return mDateFormat.format(date).toUpperCase().replace(".","");