开发者

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 开发者_如何学Golater than the date of the GregorianCalendar instance myGC2:

if (  ) {
    ...
}


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
}


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


The following should work.

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


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜