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


通过将1添加到datePicker.getMonth(),我正在获得月份数

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


但是当我删除“ 1”时,我得到的是月份数字输出

            jan-0,feb-01,mar-02

最佳答案

您想要括号。

(datePicker.getMonth()+1)


否则,您正在执行字符串连接。

例如

如果getMonth()返回0(对于一月),则

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




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


但是用原谅

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

关于android - datePicker.getMonth()+1没有给出预期的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6971439/

10-10 02:02