13

I am having trouble trying to get a time to display in 24-hour format. I have got it to display the time in another time zone but I can't get it to display in 24-hour format.

<TextClock
    android:id="@+id/hk_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:timeZone="GMT+0800"
    android:format24Hour="MMM dd, yyyy k:mm" />

Also is it possible to display a date using a TextClock?

Daniel
  • 2,355
  • 9
  • 23
  • 30
littledevils326
  • 689
  • 3
  • 11
  • 19

7 Answers7

16

If your system is in 12-hour mode, you shouldn't have to write a subclass to get what you want. The JavaDocs say:

  • In 12-hour mode:
    • Use the value returned by getFormat12Hour() when non-null
    • Otherwise, use the value returned by getFormat24Hour() when non-null
    • Otherwise, use a default value appropriate for the user's locale, such as HH:mm

Therefore, I would expect the last line in this to make getFormat12Hour() return null and thus use your custom getFormat24Hour() format:

<android.widget.TextClock
    android:id="@+id/hk_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:timeZone="GMT+0800"
    android:format24Hour="MMM dd, yyyy k:mm"
    android:format12Hour="@null" />

(BTW, as others have mentioned, forced formats should be used with care. If your users wanted a 24-hour clock, they would have specified this in their system preferences. And apps that force numerically formatted, topsy-turvy date formats on their users, like MM/dd/yyyy, invite one-star ratings (except, in this case, from US and Filipino users), so it's a good thing you're using MMM! You'd still be better off using getBestDateTimePattern(), though, which, I believe, would require subclassing TextClock.)

Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
  • Michael Scheper, I am wondering how can I remove the PM or AM from showing in my textClock? – Kala J Oct 30 '14 at 18:56
  • To see all options for the date format string: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns – Victor Rendina Jan 07 '19 at 13:59
  • 1
    @VictorRendina: Indeed. But once again, care should be taken to use those options in a locale-independent way. `h:mm a` will be confusing for users who live in places that don't use AM/PM, and if you hardcode `MM/dd/yyyy`, then about 40% of the time, users outside of the US and the Philippines simply won't be able to tell that the date they're looking at has a foreign format. _Billions of people will think your app is wrong or confusing,_ so in most cases, you should use `full`, `long`, `short`, etc., instead of custom, hard-coded formats. – Michael Scheper Jan 08 '19 at 02:17
8

My device using 12h format. To force it to use 24h format, I write this:

textClock.setFormat12Hour("kk:mm");
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dewo
  • 81
  • 1
  • 3
3

instead of set time format in xml i'm suggest to use with java file i.e

mTextClock.setFormat24Hour(format); 
Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
3

For Kotlin

I used this line for making time format to 24-Hours and it's working fine.

mTextClock.format12Hour = "kk:mm"
Ghayas
  • 1,266
  • 10
  • 17
2

Hope this will help you:

<TextClock
    android:id="@+id/tc_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:format12Hour="h:mm:ss a - d MMM, yyyy"
    android:format24Hour="k:mm:ss - d MMM, yyyy"
/>
AI Shakil
  • 1,015
  • 10
  • 20
1

Whether it shows the time in 12- or 24-hour format depends on the system setting. Perhaps your Android device or emulator is set to 12-hour format. That is why your android:format24Houris ignored. If you would like to show the 24-hour format by overwriting the system setting (which I would not recommend), add android:format12Hour="MMM dd, yyyy k:mm" as another attribute.

However, it is always recommended to show the time and date according to the system setting. If there are some other (e.g. layout) constraints in your app that require separate handling of 12- or 24-hour mode, you can check the setting dynamically in code by calling is12HourModeEnabled() or is24HourModeEnabled() on your TextClock.

To your second question, the docs say

TextClock can display the current date and/or time as a formatted string.

and that's what it is capable of -- display a the time and date. If you mean "another date than the current", you would have to stick to a simple TextView and format the date String manually.

saschoar
  • 8,150
  • 5
  • 43
  • 46
0

1st question:

I've just checked it and when on your device is set:

  • 12-h format(US, GB) - TextClock is 12h
  • 24-h format(Europe) - TextClock is 24h

As mentioned above, it's better when you'll give the choice to the user and you won't force him to use ex. your favorite format.

2nd question:

Yes, it is. You just need to use "android:format12Hour" and/or "android:format24Hour".

android:format12Hour="MMM dd, yyyy"
android:format24Hour="MMM dd, yyyy"

MMM means 3 characters for a month's name

dd means 2 characters for a day

yyyy means 4 characters for a year

Mike
  • 373
  • 4
  • 10