Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (3.5k points)

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?

1 Answer

0 votes
by (46k points)

The JDK Date API is horribly crushed, sadly. I suggest applying the Joda Time library.

Joda Time has a theory of time Interval:

Interval interval = new Interval(oldTime, new Instant());

Joda holds two thoughts: Interval for describing an interval of time within two-time instants (represent the time between 8 am and 10 am), and a Span that denotes a length of time without the actual time boundaries (e.g. represent two hours!)

If you just worry about time observations, most Date implementations (including the JDK one) performs Comparable interface which allows you to use the Comparable.compareTo()

Related questions

0 votes
1 answer
asked Jul 24, 2019 in Java by Nigam (4k points)
0 votes
1 answer
asked Dec 2, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...