user761497
user761497

Reputation: 67

Comparing Dates on Java

could everyone help me with if statement below so that it will be true when the date in the GregorianCalendar instance myGC1 is not later than the date of the GregorianCalendar instance myGC2:

if (  ) {
    ...
}

Upvotes: 0

Views: 651

Answers (5)

mre
mre

Reputation: 44250

Use the inherited Calendar method after

if(!myGC1.after(myGC2)){
    // do stuff
}

Also, according to the API, this method is equivalent to compareTo

if(!(myGC1.compareTo(myGC2) > 0)){
    // do stuff
}

Upvotes: 4

tschaible
tschaible

Reputation: 7695

if ( !(myGC1.compareTo(myGC2)>0) )
{
 ...
}

Using the compareTo method will allow you to check for equality as well as >,<.

Upvotes: 0

Ian McLaird
Ian McLaird

Reputation: 5585

if (!myGC1.after(myGC2)) {
    // do something
}

Upvotes: 2

kuriouscoder
kuriouscoder

Reputation: 5582

I prefer Joda. It makes datetime comparison very straightforward and easy without having to deal with calendar specifics.

DateTime firstDate = ...;
DateTime secondDate = ...;

return firstDate.compareTo(secondDate);

Upvotes: 0

KappaMax
KappaMax

Reputation: 324

The following should work.

if ( !gc2.after(gc) )
{
  // then the date is not after gc1.. do something
}

Upvotes: 1

Related Questions