Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
        date.setTime(time.toMillis(true));

        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?

like image 268
Dale Julian Avatar asked May 18 '16 02:05

Dale Julian


1 Answers

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(".","");
like image 137
MiltoxBeyond Avatar answered Oct 18 '22 21:10

MiltoxBeyond