kk和HH在日期格式java中的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22835041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between kk and HH in date formatting java
提问by Ron Jobbins
I'm pretty new to java and am trying to format a time using 24 hour format. I've come across two ways of formatting the hour - HH and kk:
我对 Java 很陌生,正在尝试使用 24 小时格式来格式化时间。我遇到过两种格式化小时的方法 - HH 和 kk:
SimpleDateFormat format1 new SimpleDateFormat("HH:mm");
SimpleDateFormat format2 new SimpleDateFormat("kk:mm");
Date date = new Date();
System.out.println(format1.format(date));
System.out.println(format2.format(date));
These both produce something like 11:21. What's the difference between them? Am I missing something?
这些都产生类似 11:21 的东西。它们之间有什么区别?我错过了什么吗?
采纳答案by Josh Roberts
The two formats essentially do the same thing but differ in how they handle midnight
. kk
will format midnight
to 24:00
whereas HH
will format to 00:00
. The hours in a day in k
are 1-24
and in H
are 0-23
这两种格式本质上做同样的事情,但处理方式不同midnight
。kk
将格式化midnight
为,24:00
而HH
将格式化为00:00
. 在一天的时间中k
是1-24
和H
是0-23
It's always worth checking the java documentationas it generally provides very useful explanations as well as examples of uses.
始终值得检查java 文档,因为它通常提供非常有用的解释以及使用示例。
回答by Evgeniy Dorofeev
try this to see the difference
试试这个看看区别
SimpleDateFormat format1 = new SimpleDateFormat("HH:mm");
SimpleDateFormat format2 = new SimpleDateFormat("kk:mm");
Date date = new GregorianCalendar(2001, 0, 1, 0, 0 , 0 ).getTime();
System.out.println(format1.format(date));
System.out.println(format2.format(date));
output
输出
00:00
24:00