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
DateAPI is horribly broken unfortunately. I recommend using Joda Time library.Joda Time has a concept of time Interval:
EDIT: By the way, Joda has two concepts:
Intervalfor representing an interval of time between two time instants (represent time between 8am and 10am), and aDurationthat represents a length of time without the actual time boundaries (e.g. represent two hours!)If you only care about time comparisions, most
Dateimplementations (including the JDK one) implementsComparableinterface which allows you to use theComparable.compareTo()