如何仅根据提供的小时计算Java中经过的时间
这有两个部分:首先计算开始日期。然后计算经过的时间。对于第二部分,我在这里找到了一些很好的建议: Calculate elapsed time in Java / Groovy< /a>
然而,第一部分对我来说是个问题。它的工作原理如下: 用户指定时间跨度从小时值(00 到 23)。我没有任何类型的开始日期/时间对象 - 只是一个整数小时。由此我需要计算开始日期(然后是经过的时间)。
如果开始时间大于当前时间,则为前一天。然而,为了从中获得实际的开始日期,我需要考虑月份和年份的边界以及闰年和夏令时变化等因素。肯定已经有人解决了这样的问题。 (我相信这可能相当复杂。)是否有一个经过验证的解决方案可以让我计算从一天中的给定时间(00 到 24)到现在实际经过了多少时间(以秒为单位)? (开始时间将始终假定为整点。)
There are two parts to this: first compute the start date. Then compute the elapsed time. For the second part, I found some good advice here: Calculate elapsed time in Java / Groovy
However, the first part is a problem for me. Here's how it works:
The user indicates that the time span extends from an hour value (00 to 23). I do not have a starting date/time object of any sort - just an integer hour. From that I need to figure the start date (and then the elapsed time).
If the start hour is greater than the now hour, it was the prior day. In order to get an actual start date from that however, I need to potentially consider month and year boundaries as well as things like leap years and daylight savings time changes. Surely someone has solved a problem like this already. (I believe it can be quite complex.) Is there a proven solution that will let me compute how much time (in seconds) has actually elapsed from the given hour of the day (00 to 24) to now? (The start time will always be assumed to be on the hour.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我建议使用 Joda Time API。在我看来,它是 Java 可用的最佳日期/时间 API。
接下来,您需要弄清楚在各种极端情况下要做什么。特别是,假设用户输入“1”并且您即将进入夏令时过渡。凌晨 1 点可能发生了两次(如果时间因为从 DST 向后过渡而变为 1:58、1:59、1:00、1:01),或者没有发生完全没有(如果由于转换为 DST 而时间变为 12:58、12:59、2:00)。您需要弄清楚在每种情况下该怎么做 - 请记住,这也意味着要了解时区。
一旦你解决了这个问题,它可能不会太难。使用 Joda Time,您可以使用
withHourOfDay
方法从一个时间转到另一个时间,并设置时间的一个组成部分 - 同样,如果需要,也有简单的 API 用于添加或减去一天。然后,您可以非常轻松地计算出两个DateTime
值之间的时间 - 同样,Joda Time 提供了您所需的一切。下面是一个示例,它没有尝试对 DST 转换执行任何操作,但它是一个很好的起点:
Firstly, I'd suggest using the Joda Time API. It's the best date/time API available for Java, in my opinion.
Next you need to work out exactly what to do in various corner cases. In particular, suppose the user enters "1" and you're near a daylight saving transition. It's possible that 1am happened twice (if the the time went 1:58, 1:59, 1:00, 1:01 because of a transition back away from DST) or that it didn't happen at all (if the time went 12:58, 12:59, 2:00 because of a transition forward into DST). You need to work out what to do in each of those situations - and bear in mind that this means knowing the time zone too.
Once you've worked that out, it may not be too hard. With Joda Time you can use
withHourOfDay
method to get from one time to another having set one component of the time - and likewise there are simple APIs for adding or subtracting a day, if you need to. You can then work out the time between twoDateTime
values very easily - again, Joda Time provides everything you need.Here's an example which doesn't try to do anything with DST transitions, but it's a good starting point:
java.time
接受的答案提供了使用 Joda-Time API 的解决方案,这可能是最好的第 3 方日期时间 API当时在Java中。 2014 年 3 月,现代日期时间 API 作为 Java 8 标准库的一部分发布。请注意 Joda-Time 主页上的以下消息:
使用现代日期时间 API
java.time
的解决方案:示例运行的输出:
在线演示
从跟踪:日期时间。
java.time
The accepted answer has provided a solution using Joda-Time API which was probably the best 3rd party date-time API in Java at that time. In Mar 2014, the modern date-time API was released as part of the Java 8 standard library. Notice the following message on the Home Page of Joda-Time:
Solution using
java.time
, the modern date-time API:Output from a sample run:
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
假设
startHour
由用户指定(假设在 0-23 范围内)。现在你可以从这个开始:Let's say
startHour
is given by the user (assume, that's in the 0-23 range). Now you may start with this:使用 Joda-Time 类。你可以这样做:
Use Joda-Time classes. You can do something like this: