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?

有帮助吗?

解决方案 3

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.

其他提示

Easier solution is:

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

This correctly handles 12/24 hour user setting.

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 .

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.

String timestamp = (DateFormat.format("yyyy.MM.dd kk:mm:ss", new Date())).toString();

Instead of hh, use kk.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top