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
5 Answers 5
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
mre
44.4k33 gold badges122 silver badges170 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Ian McLaird
"not later" is not the same as before.
mre
@Ian, right, I am missing the myGC1 == myGC2. a simple comment would have sufficed, not sure why I deserved the -1 though...whatever.
Ian McLaird
you're right, the downvote wasn't really deserved. I tried to undo it, but it wouldn't let me do so. Sorry :(
if (!myGC1.after(myGC2)) {
// do something
}
answered May 19, 2011 at 17:19
Ian McLaird
5,5852 gold badges25 silver badges31 bronze badges
Comments
The following should work.
if ( !gc2.after(gc) )
{
// then the date is not after gc1.. do something
}
Comments
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
kuriouscoder
5,6528 gold badges30 silver badges41 bronze badges
Comments
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
tschaible
7,6951 gold badge33 silver badges35 bronze badges
1 Comment
ratchet freak
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)
lang-java