计算两个Java日期实例之间的差异 [英] Calculating the difference between two Java date instances
问题描述
(new java.util.Date())。getTime ) - oldDate.getTime()
然而,这只是让我有一个长的表示毫秒。有没有更简单,更好的方式获得时间增量?
JDK Date
API令人遗憾的是坏了。我建议您使用 Joda时间库。
Joda Time有一个时间概念 Interval :
间隔间隔=新间隔(oldTime,新Instant());
编辑:顺便说一句,Joda有两个概念: Interval
表示两个时间之间的时间间隔(表示上午8点到10点之间的时间),以及持续时间
,表示没有实际时间边界的时间长度(例如代表两个小时!)
如果您只关心时间比较,大多数 Date
实现(包括JDK一个)实现可比较的
接口,允许您使用 Comparable.compareTo()
I'm using Java's java.util.date class in Scala and want to compare a date object and the current time. I know I can calculate the delta by using getTime():
(new java.util.Date()).getTime() - oldDate.getTime()
However, this just leaves me with a Long representing milliseconds. Is there any simpler, nicer way to get a time delta?
The JDK Date
API is horribly broken unfortunately. I recommend using Joda Time library.
Joda Time has a concept of time Interval:
Interval interval = new Interval(oldTime, new Instant());
EDIT: By the way, Joda has two concepts: Interval
for representing an interval of time between two time instants (represent time between 8am and 10am), and a Duration
that represents a length of time without the actual time boundaries (e.g. represent two hours!)
If you only care about time comparisions, most Date
implementations (including the JDK one) implements Comparable
interface which allows you to use the Comparable.compareTo()
这篇关于计算两个Java日期实例之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!