6

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?

Dale Julian
  • 1,560
  • 18
  • 35

2 Answers2

6

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(".","");
MiltoxBeyond
  • 2,683
  • 1
  • 13
  • 12
0
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentDateandTime = sdf.format(new Date());
Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • It still differs between Nexus 10 and Nexus 5X. Nexus 10 formats it to "AM" or "PM" while Nexus 5X formats it to "a.m." or "p.m.". I guess I would just have to manually format this. – Dale Julian May 18 '16 at 03:48
  • "AM" and "a.m" are related to system configuration so you can do it manually to show either in "AM" or "a.m", but it is not recommended – Mrugesh May 18 '16 at 04:01