Android DateFormat for AM / PM is different between devices

I format the dates as follows:

    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 use:

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

But it is different between the two devices that I use for testing ...

Nexus 10: Nexus 10

Nexus 5X: Nexus 5X

How can I uniformly format it between devices?

+4
source share
2 answers

You may need either a 24-hour value to determine what to add so that you can add the desired format.

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 simply do without changing the TIME value:

return mDateFormat.format(date).toUpperCase().replace(".","");
+3
source
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentDateandTime = sdf.format(new Date());
0
source

All Articles