0

Currently I'm using new SimpleDateFormat("h:mm a", Locale.getDefault()) to display time in my app. It always shows time like 12:00 AM either the device language is set to English (US) or English (UK). I would like to change this behaviour and display time in 24-h format for devices which language is set to English (UK). Is there a good way?

Artem M
  • 1,026
  • 14
  • 24

2 Answers2

1

Use DateFormat.getDateInstance(int style, Locale locale)

Malik Khan
  • 74
  • 6
  • 1
    Also you can visit similar problem here. https://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string – Malik Khan Jul 13 '18 at 21:32
0

Use this [Simple and understandable :) ] :

final Calendar calendar = Calendar.getInstance();
        String locale = Locale.getDefault().toString();

        if (locale.equals("en_US")) {

            SimpleDateFormat US = new SimpleDateFormat("h:mm a");
            String us = US.format(calendar.getTime());
            Log.i ("US - Time is : ",us);

        } else if (locale.equals("en_GB")) {

            SimpleDateFormat UK = new SimpleDateFormat("hh:mm:ss");
            String uk = UK.format(calendar.getTime());
            Log.i ("UK - Time is : ",uk);

        } else {

            Log.i("Your Location ",locale);

        }}

I've created a conditions that can change the time according to the location [Locale] ! hope to be useful.

Good luck.

Siros Baghban
  • 406
  • 4
  • 7
  • Though this way does what I want, I would avoid using such comparisons with raw string constants since there could be other locales (not only US and UK) and I'd like to display dates considering rules for each locale. Sorry, I should have mentioned this in my question. – Artem M Jul 13 '18 at 22:42