9

I'm trying to know if, from the Locale, I have to use the 24 or the 12 format.

I found this:

if (android.text.format.DateFormat.is24HourFormat(getApplicationContext()))

But this returns a boolean according to the system, not to the Locale. This means that if the user use english for the app but has a phone using the 24hours format, it will return true...

How can I get a boolean telling me if the format to use is, or not, 24hours? I'm not using Joda. Thanks in advance.

EDIT as it seems that some people think that's a duplicate, I already read the posts you provided as a source for the duplicate flag. It was only solved with approximated workarounds or method providing a date, most of the time the current one, even if it's in the corresponding format.

As you can guess, if I'm asking for a way to know whether I should use the 12 hours format or not according to the Locale, it means that I don't need the date or anything, that I already have, but a confirmation about the format I have to use.

Virthuss
  • 3,142
  • 1
  • 21
  • 39
  • 2
    Wouldn't you want to display the time in the user's preferred format, regardless of location? – Mike M. Jan 06 '16 at 08:40
  • 2
    Why not follow the users choice? – Andreas Jan 06 '16 at 08:40
  • The choice is not mine, and it's also a good question as I havent see any clear answer for that yet, let's say – Virthuss Jan 06 '16 at 08:42
  • `java.text.DateFormat.getTimeInstance(int style, Locale locale)` - http://stackoverflow.com/questions/5058880/date-and-time-formatting-depending-on-locale – Mike M. Jan 06 '16 at 09:03
  • Thanks but this is sadly not answering the question: I already have a time and I don't need the current one; + I just need to know weather I should use the 12 or 24 format. A method returning a boolean like is24HourFormat but for the Locale is what I'm looking for. – Virthuss Jan 06 '16 at 09:08
  • That method doesn't have the best name, but it doesn't return an anything with a specific time or date. It returns the `DateFormat` instance for the given locale, which you would then use to format whatever `Date` object you need. – Mike M. Jan 06 '16 at 09:17
  • Thanks but I don't need to format something. I need to know whether, according to the Locale, I should use the 12hours format or not. – Virthuss Jan 06 '16 at 09:20
  • 1
    Well, then the second option listed in the accepted answer in Manu's link is probably as good as you can do, though I don't know why you'd just need to know which format to use without actually using it. – Mike M. Jan 06 '16 at 09:26
  • No I will make an use of it, but with some variables from my code. – Virthuss Jan 06 '16 at 09:27
  • Then I don't understand what the problem is. The `getTimeInstance()` method returns the `DateFormat` instance that you would use with whatever time value you give it. Like I said, it's not giving you anything with a time already set; just the correct format object for the given locale. – Mike M. Jan 06 '16 at 09:30
  • How can I know if it's the 12 or the 24 hours format from that? – Virthuss Jan 06 '16 at 09:35
  • @Virthuss Are you setting the `Locale` from your app ? – Sharp Edge Jan 06 '16 at 09:54

1 Answers1

5

Taking a look at the source code of the actual android.text.format.DateFormat.is24HourFormat method you'll see how they do it:

    Locale locale = context.getResources().getConfiguration().locale;

    java.text.DateFormat natural =
        java.text.DateFormat.getTimeInstance(
            java.text.DateFormat.LONG, locale);

    if (natural instanceof SimpleDateFormat) {
        SimpleDateFormat sdf = (SimpleDateFormat) natural;
        String pattern = sdf.toPattern();
        if (pattern.indexOf('H') >= 0) {
            value = "24";
        } else {
            value = "12";
        }
    } else {
        value = "12";
    }

So you can adapt it to the way you get the locale.

carrizo
  • 689
  • 6
  • 15