daaatz
daaatz

Reputation: 394

Java.util.Date compare ">="

In java.util.Date the function after() == ">"

Is there a way to compare dates as ">=" ?

Upvotes: 1

Views: 10854

Answers (5)

Philipp Wendler
Philipp Wendler

Reputation: 11433

Date implements Comparable, so you can use the compareTo method:

if (date.compareTo(otherdate) >= 0) { ... }

Basically compareTo is the Java way for comparing objects with >, <= etc. and works in lots of circumstances. The after and before methods of Date probably exist only because they were introduced before compareTo was added.

Upvotes: 1

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

No ....Java does not support that...instead you can use

Date#equals() and Date#after()

I too also prefer Joda Time

Upvotes: 0

Nikita Skvortsov
Nikita Skvortsov

Reputation: 4923

What about "not before"?

boolean result = !date.before(otherDate)

Also, Joda Time can save you a lot of time :)

Upvotes: 2

drew moore
drew moore

Reputation: 32670

if (!date.before(otherdate) - has the same effect as " not after or equal to"

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272217

How about using

!thisDate.before(thatDate)

to implement the ">=" function. Not hugely nice, I appreciate.

Upvotes: 10

Related Questions