简体   繁体   English

Java简单日期格式时间问题

[英]Java Simple Date Format Time Issues

SimpleDateFormat sdf1 = new SimpleDateFormat(yyyy-MM-dd kk:mm:ss);
SimpleDateFormat sdf2 = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
Calendar cal = Calendar.getInstance();
Date d = cal.getTime();
System.out.println("Current Time is:"+d);
System.out.println("Time value using kk:" + sdf1.format(d));
System.out.println("Time Value using HH:" + sdf2.format(d));

Result:
Current Time is: Wed Sep 25 00:55:20 IST 2013
Time value using kk : 2013-09-25 24:55:20
Time value using HH : 2013-09-25 00:55:20

Can anyone tell why this behavior changes in the time, when using kk and HH. 任何人都可以告诉为什么当使用kk和HH时,这种行为会在时间上发生变化。

and also kk gives 24:55:20, is this be useful any where. 并且kk给出24:55:20,这在任何地方都是有用的。 To my knowledge, there is only 00:00:00 to 23:59:59 is the time range. 据我所知,时间范围只有00:00:00到23:59:59。

Is there any beyond this range, if so where is the place "kk" will be useful? 有没有超出这个范围,如果是这样的地方“kk”将有用吗?

From the JavaDocs ... 来自JavaDocs ......

k - Hour in day (1-24) k - 白天的小时(1-24)
H - Hour in day (0-23) H - 白天小时(0-23)

Updated 更新

As for why this would be need/supported...There are any number of reasons, some which might be... 至于为什么需要/支持这个......有很多原因,有些可能是......

  • Providing support for external systems. 为外部系统提供支持。 This makes it possible to parse date/time strings from a variety of sources, providing the API with the flexibility to adapt to the needs of the developer. 这使得可以从各种来源解析日期/时间字符串,从而为API提供了适应开发人员需求的灵活性。
  • I don't know about you, but my alarm clock works in 1-24 (1-12) hour mode ;) 我不了解你,但我的闹钟工作在1-24(1-12)小时模式;)

I would suggest the intention was to try and meet as many of the possible formats that the API may encounter/require without attempting to limit the developer or require us to have to write our own API's. 我建议的目的是尝试满足API可能遇到/要求的许多可能格式,而不试图限制开发人员或要求我们必须编写自己的API。

I don't know about you, but I have lots of other things I need to do ;) 我不了解你,但我还有很多其他需要做的事情;)

This is documented in the SimpleDateFormat docs: 这在SimpleDateFormat文档中有记录:

H Hour in day (0-23) H小时(0-23)
k Hour in day (1-24) k小时(1-24)
K Hour in am/pm (0-11) 上午/下午K小时(0-11)
h Hour in am/pm (1-12) h上午/下午(1-12)

So, you have 4 different formats for hours. 因此,您有4种不同的格式。 I guess addition of k and h was a mistake. 我想添加kh是一个错误。 They really don't make any sense, and almost never needed. 他们真的没有任何意义,几乎从不需要。

Use H for 24-hour formats, and K for 12-hour format. 使用H表示24小时格式,使用K表示12小时格式。

There's no reason why there should be an error. 没有理由应该出现错误。 k is documentedin SimpleDateFormat as a field for: SimpleDateFormat中将k 记录为以下字段:

Hour in day (1-24) 一天中的小时(1-24)

That explains why you get 24 for midnight instead of 00, too. 这就解释了为什么你的午夜时间为24而不是00。 If you don't want values between 01 and 24, don't use kk in your format string! 如果您不想要 01到24之间的值,请不要在格式字符串中使用kk I can't remember the last time I wanted to use something like that, but presumably it's used in some cases (particularly for values which are always "on the hour" so you get 24:00 instead of 00:00). 我不记得上次我想要使用类似的东西,但可能在某些情况下使用它(特别是对于总是“在一小时内”的值,所以你得到24:00而不是00:00)。

Always consult the docs for what format strings mean. 始终查阅文档,了解字符串的含义。

[Kian Fatt, Ting] Buddy the difference is that if you use capital H is zero to 23 (0-23) , but if you use small letter k is one to 24 (1-24) . [Kian Fatt,Ting] Buddy的不同之处在于,如果你使用大写字母H is zero to 23 (0-23) ,但如果你使用小写字母k is one to 24 (1-24) The only difference is this and both is right in showing time. 唯一的区别是这一点,两者都是正确的时间。 You can look at the java documentation link I have provided below. 您可以查看我在下面提供的java文档链接。

Souce: SimpleDateFormat Souce: SimpleDateFormat

If you can don't use dates and calendar... it's easy to do mistakes with this classes. 如果你不能使用日期和日历...这个类很容易出错。

jodatime it's a better solution jodatime这是一个更好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM