7

In my app I have an option to pause execution for a certain amount of time. I want to show the time when the execution will resume, including seconds, and I want the time string to be formatted according to sustem settings. This is the code I came up with:

long millis = getResumeTime();
String timeString;
timeString = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM).format(millis);

This does produce a formatted string with seconds, but it returns AM/PM-formatted time, even though I have set 24-hour time format in settings. It's even funnier since the time in the system tray is correctly formatted using 24 hour format.

I tried using DateFormat.getTimeFormat like this:

long millis = getResumeTime();
String timeString;
java.text.DateFormat df = android.text.format.DateFormat.getTimeFormat(this);
timeString = df.format(millis);

But the resulting string does not contain seconds, and I don't see a way to include them.

I'm running this code on Android 4.2 emulator. Am I missing something here? Is SimpleDateFormat not aware of 12/24 hour setting? If not, how do I get a string representation of time(including hours, minutes and seconds) in system format?

Darwind
  • 7,284
  • 3
  • 49
  • 48
Anton Cherkashyn
  • 5,719
  • 6
  • 43
  • 80

5 Answers5

7

Easier solution is:

String timeString = DateUtils.formatDateTime(getContext(), timeInMillis,
           DateUtils.FORMAT_SHOW_TIME);

This correctly handles 12/24 hour user setting.

vovan888
  • 748
  • 6
  • 10
5

There seems to be a bug in Android 5 to 6.0.1 where DateFormat#getTimeInstance(int) returns a DateFormat that ignores the Use 24-hour format setting. The other answers mention a variety of workarounds for this bug. You can follow the issue on https://code.google.com/p/android/issues/detail?id=181201 .

s1m0n
  • 7,825
  • 1
  • 32
  • 45
2

It can very well depend on what locale your system is in. If your system is in US, it will default to 12h instead of 24h. i.e.

long millis = new Date().getTime();
String uk = SimpleDateFormat
               .getTimeInstance(SimpleDateFormat.MEDIUM, Locale.UK)
               .format(millis);
String us = SimpleDateFormat
               .getTimeInstance(SimpleDateFormat.MEDIUM, Locale.US)
               .format(millis);
System.out.println("UK: " + uk);
System.out.println("US: " + us);

will give you

UK: 16:19:49
US: 4:19:49 PM

So, perhaps you can grab the system locale and specify it in your formatter.

However, if you always want it in 24h format, then I suggest you explicitly specify it in your formatter.

UPDATE: Since you wanted to grab the time format based on the device specification, you could use the system's Time_12_24 value and determine your format from the resulting value.

dispake
  • 3,259
  • 2
  • 19
  • 22
  • 1
    "I suggest you explicitly specify it in your formatter" - That is certainly an option, but I was hoping there was a method already available that could do it automatically :) – Anton Cherkashyn Apr 15 '13 at 07:02
  • That suggestion was only if you want to -always- display the 24H format. Of course, you could always use the JVM's default locale and have it "automatically" determine the display format. – dispake Apr 15 '13 at 21:18
  • 3
    I don't want to -always- display the 24 hour format, but I want to display 24 hour format if the user has checked the appropriate checkbox in his device's settings. Which includes cases if the user has Locale.US. So let's say I have two cases: (1)Locale.US and 24 hour checkbox UNCHECKED, (2)Locale.US and 24 hour checkbox CHECKED. In both cases SimpleDateFormat.getTimeInstance returns hours in AM/PM format, ignoring the 24 hour system setting. – Anton Cherkashyn Apr 15 '13 at 22:00
  • 1
    I updated the answer to include the system TIME_12_24 option. Perhaps you can utilize that value in conjunction with the user's selected locale to determine your format. – dispake Apr 16 '13 at 08:15
0

You should probably create your own format pattern. If I remember correctly, the pattern you'd use is "HH:mm:ss.SSS" to give you hours, minutes, seconds, and three digits precision of fractional seconds.

See the API reference docs for SimpleDateFormat.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
0
String timestamp = (DateFormat.format("yyyy.MM.dd kk:mm:ss", new Date())).toString();

Instead of hh, use kk.

Roman C
  • 49,761
  • 33
  • 66
  • 176
jaywalk
  • 9
  • 1