如何以“yyyy-MM-dd HH:mm:ss”格式正确获取日期

use*_*779 3 android

String date = (String) android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", new java.util.Date());
Run Code Online (Sandbox Code Playgroud)

某些设备工作正常。但在其他设备中,我得到:

2015-12-24 HH:28:24

在如果我更改 (HH) - > (hh) 时不起作用的设备中。但我需要 24 小时模式的小时。

Art*_*gin 6

尝试使用kk而不是hh例如:

String date = (String) android.text.format.DateFormat.format("yyyy-MM-dd kk:mm:ss", new java.util.Date());
Run Code Online (Sandbox Code Playgroud)

或使用SimpleDateFormat

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.format(new Date(System.currentTimeMillis()));
Run Code Online (Sandbox Code Playgroud)

查看文档:SimpleDateFormat

H - 一天中的小时 (0-23)

k - 一天中的小时 (1-24)

K - 上午/下午的小时 (0-11)

h - 上午/下午的小时 (1-12)