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 ( ) {
 ...
}
Vivin Paliath
95.8k42 gold badges230 silver badges302 bronze badges
asked May 19, 2011 at 17:15
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
}
answered May 19, 2011 at 17:17
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
}
answered May 19, 2011 at 17:19

Comments

1

The following should work.

if ( !gc2.after(gc) )
{
 // then the date is not after gc1.. do something
}
answered May 19, 2011 at 17:19

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);
answered May 19, 2011 at 17:22

Comments

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

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

answered May 19, 2011 at 17:18

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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.