0

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 (  ) {
    ...
}
0

5 Answers 5

4

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

"not later" is not the same as before.
@Ian, right, I am missing the myGC1 == myGC2. a simple comment would have sufficed, not sure why I deserved the -1 though...whatever.
you're right, the downvote wasn't really deserved. I tried to undo it, but it wouldn't let me do so. Sorry :(
2
if (!myGC1.after(myGC2)) {
    // do something
}

Comments

1

The following should work.

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

Comments

0

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);

Comments

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

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

1 Comment

you should compare the result of compareTo with 0 not 1 or -1 (it might just return the number of milliseconds myGC1 is away from myGC2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.