SimpleDateFormat 上的 java HH:mm 和 hh:mm 之间的区别

2022-01-15 00:00:00 format date java

kk:mm、HH:mm 和 hh:mm 格式有什么区别??

Whats the difference between kk:mm, HH:mm and hh:mm formats ??

    SimpleDateFormat broken = new SimpleDateFormat("kk:mm:ss");
    broken.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
    SimpleDateFormat working = new SimpleDateFormat("HH:mm:ss");
    working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
    SimpleDateFormat working2 = new SimpleDateFormat("hh:mm:ss");
    working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

    System.out.println(broken.format(epoch));
    System.out.println(working.format(epoch));
    System.out.println(working2.format(epoch));

打印:

24:00:00
00:00:00
05:30:00

推荐答案

kk: (01-24) 看起来像 01, 02..24.

kk: (01-24) will look like 01, 02..24.

HH:(00-23) 看起来像 00, 01..23.

HH:(00-23) will look like 00, 01..23.

hh:(上午/下午的 01-12)看起来像 01、02..12.

hh:(01-12 in AM/PM) will look like 01, 02..12.

所以最后的打印输出 (working2) 有点奇怪.应该说 12:00:00(如果你设置 working2 时区和格式,(正如 kdagli 指出的那样)你不是)

so the last printout (working2) is a bit weird. It should say 12:00:00 (edit: if you were setting the working2 timezone and format, which (as kdagli pointed out) you are not)

相关文章