开发者

How to test for am or pm?

I am trying to test for am or pm in a if else statement..

if(am){
//Do something
else{

//Do something else

Ive tried

int am 开发者_JAVA百科= cld.get(Calendar.AM_PM);

but the if else

wont take it as a parameter to test. Maybe because its not boolean.

How would i go about testing this?


You're correct that the if-else won't accept it because it is not boolean. Calendar.AM_PM only ever holds the value 0 or 1. A language like C would accept 0 or 1 as boolean; Java won't.

You want to do something more like this:

int am = cld.get(Calendar.AM_PM);
if (am == 0) {
    // Do whatever for the AM
} else {
    // Do whatever because it must be PM
}


Surely your if clause cannot accept integer as it is. You need something (comparison perhaps) to get boolean out of it.

   if(am > 0)
    {
      //its PM
    else { //its AM }


Calendar.AM_PM is an int. To evaluate it in an if statement, cast it to a boolean:

if((bool)am) {
   //It's AM
} else {
   //It's PM
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜