如果把日期条件放在android之后if

时间:2013-09-12 12:59:01

标签: android date

我做了一个应用程序,然后把(如果)这样的DAY_OF_MONTH条件放在

之后
if (cal.get(Calendar.DAY_OF_MONTH) == 9) {
        Intent intent1 = new Intent(MainActivity.this, TextActivity.class);
        intent1.putExtra("key", getResources().getString(R.string.s_monday_txt));
        startActivity(intent1);
        finish();
    } else if (cal.get(Calendar.DAY_OF_MONTH) == 10) {
        Intent intent2 = new Intent(MainActivity.this, TextActivity.class);
        intent2.putExtra("key", getResources().getString(R.string.s_tuesday_txt));
        startActivity(intent2);
        finish();
    }

在另一个地方我把(如果)这样的MONTH条件放在

之后
    if (cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) {
        Intent intent1 = new Intent(MainActivity.this, TextActivity.class);
        intent1.putExtra("key", getResources().getString(R.string.s_september_txt));
        startActivity(intent1);
        finish();
    } else if (cal.get(Calendar.MONTH) == Calendar.AUGUST) {
        Intent intent2 = new Intent(MainActivity.this, TextActivity.class);
        intent2.putExtra("key", getResources().getString(R.string.s_august_txt));
        startActivity(intent2);
        finish();
    }

这对我来说很好,但问题是如何在同一时间检查这两个条件

if (cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) + (cal.get(Calendar.DAY_OF_MONTH) == 9) {
        Intent intent1 = new Intent(MainActivity.this, TextActivity.class);
        intent1.putExtra("key", getResources().getString(R.string.s_september_txt));
        startActivity(intent1);
        finish();
    } else if (cal.get(Calendar.MONTH) == Calendar.AUGUST)  + (cal.get(Calendar.DAY_OF_MONTH) == 10) {
        Intent intent2 = new Intent(MainActivity.this, TextActivity.class);
        intent2.putExtra("key", getResources().getString(R.string.s_august_txt));
        startActivity(intent2);
        finish();
    }

我试过(+),(和),(||),(,)所有这些都行不通 任何帮助???

3 个答案:

答案 0 :(得分:0)

在java中,+符号是一个补充。如果你想要“和”的意思,请使用&&标志。

&安培;&安培; - 和

|| - 或

所以如果你想检查if语句中的两个条件是否都适用,那么就像这样使用它:

if (value == 1 && value2 == 1){
 // do something if value is equal to 1 _AND_ value2 equals to 1
}

答案 1 :(得分:0)

更改为&&将其更改为||

cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) + (cal.get(Calendar.DAY_OF_MONTH) == 9

将其更改为:

cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) && (cal.get(Calendar.DAY_OF_MONTH) == 9

答案 2 :(得分:0)

以下是&&||的含义。根据您的要求使用运营商

if ((cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) && (cal.get(Calendar.DAY_OF_MONTH) == 9)) {
 // it is  september and day of month is 9
}


if ((cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) || (cal.get(Calendar.DAY_OF_MONTH) == 9)) {
 // either september or 9
}

参考link