gpt4 book ai didi

java - 字符串日期不正确

转载 作者:行者123 更新时间:2023-11-29 03:36:35 28 4
gpt4 key购买 nike

可能会有简单快速的答案,但我仍然无法找出为什么结果是

Date date = new Date(60000); //one min.
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String dateStr = dateFormat.format(date);

dateStr - 01:01:00还有一个小时。时区?没有它我怎么设置它?谢谢。

最佳答案

Date 代表一个特定的时刻,而不是持续时间。 new Date(60000) 不会创建“一分钟”。查看docs for that constructor :

Initializes this Date instance using the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.

如果您想要“从现在起一分钟”,您可能需要使用 Calendar 类,特别是 add方法。

更新:

DateUtils有一些您可能会觉得有用的有用方法。如果你想要 HH:mm:ss 格式的耗时,你可以尝试 DateUtils.formatElapsedTime .像这样的东西:

String dateStr = DateUtils.formatElapsedTime(60);

请注意,60 是以秒为单位。

关于java - 字符串日期不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15213011/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com