SimpleDateFormat上的java HH:mm和HH:mm的区别

时间:2022-04-29 22:04:01

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

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

    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));

prints:

打印:

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

3 个解决方案

#1


37  

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

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

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

(00-23)看起来像00 01. 23。

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

hh:(01-12 in AM/PM)看起来像01、02…

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)

所以最后的打印输出(working2)有点奇怪。它应该说12:00:00(编辑:如果您设置了working2时区和格式,这是(正如kdagli指出的)您不是)

#2


16  

Please take a look here

请看这里

HH is hour in a day (starting from 0 to 23)

HH是一天一小时(从0到23)

hh are hours in am/pm format

hh是am/pm格式的小时

kk is hour in day (starting from 1 to 24)

kk是一天中的一小时(从1点到24点)

mm is minute in hour

mm是一分钟一小时

ss are the seconds in a minute

ss是一分钟的秒

#3


16  

Actually the last one is not weird. Code is setting the timezone for working instead of working2.

实际上最后一个并不奇怪。代码正在设置工作的时区,而不是工作2。

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

SimpleDateFormat working2 =新的SimpleDateFormat(“hh:mm:ss”);working.setTimeZone(TimeZone.getTimeZone("等/ UTC "));

kk goes from 1 to 24, HH from 0 to 23 and hh from 1 to 12 (AM/PM).

kk从1到24,HH从0到23,从1到12 (AM/PM)。

Fixing this error gives:

修复这个错误给:

24:00:00
00:00:00
01:00:00

#1


37  

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

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

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

(00-23)看起来像00 01. 23。

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

hh:(01-12 in AM/PM)看起来像01、02…

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)

所以最后的打印输出(working2)有点奇怪。它应该说12:00:00(编辑:如果您设置了working2时区和格式,这是(正如kdagli指出的)您不是)

#2


16  

Please take a look here

请看这里

HH is hour in a day (starting from 0 to 23)

HH是一天一小时(从0到23)

hh are hours in am/pm format

hh是am/pm格式的小时

kk is hour in day (starting from 1 to 24)

kk是一天中的一小时(从1点到24点)

mm is minute in hour

mm是一分钟一小时

ss are the seconds in a minute

ss是一分钟的秒

#3


16  

Actually the last one is not weird. Code is setting the timezone for working instead of working2.

实际上最后一个并不奇怪。代码正在设置工作的时区,而不是工作2。

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

SimpleDateFormat working2 =新的SimpleDateFormat(“hh:mm:ss”);working.setTimeZone(TimeZone.getTimeZone("等/ UTC "));

kk goes from 1 to 24, HH from 0 to 23 and hh from 1 to 12 (AM/PM).

kk从1到24,HH从0到23,从1到12 (AM/PM)。

Fixing this error gives:

修复这个错误给:

24:00:00
00:00:00
01:00:00