1
             Toast.makeText(getBaseContext(),
            "Date selected:" + datePicker.getMonth()+1+
            "/"+ datePicker.getDayOfMonth() +
            "/"+ datePicker.getYear() +"\n" +
            "Time Slected:" + timePicker.getCurrentHour() + 
            ":"+ timePicker.getCurrentMinute(),
            Toast.LENGTH_SHORT).show();

datePicker.getMonth()に1を追加することで、月番号を取得しています

            output like->
            jan-01,feb-11,mar-21

しかし、「1」を削除すると、次のような月番号が出力されます。

            jan-0,feb-01,mar-02
4

2 に答える 2

7

かっこが必要です。

(datePicker.getMonth()+1)

それ以外の場合は、文字列の連結を行っています。

例えば

0(1月の場合getMonth())を返す場合、

"Date selected: " + datePicker.getMonth()+1

("Date selected: " + 0) + 1
= "Date selected: 0" + 1
= "Date selected: 01"

しかし、parensで

"Date selected: " + (datePicker.getMonth()+1)
= "Date selected: " + (0+1)
= "Date selected: " + 1
= "Date selected: 1"
于 2011-08-07T07:48:09.383 に答える
2

月は0から始まるインデックスが付けられます。したがって、最初の月は0になり、2番目の月は1になります。

于 2011-08-07T07:49:37.533 に答える