Date picker shows wrong month [duplicate]

删除回忆录丶 提交于 2020-04-17 19:18:47

问题


Hey guys can any tell me what can I do to get the right number of month I am selecting, no matter which month I select, it always shows one less in text field for e.g if i select 1st of january 1997 and press ok it will show the date like this : 1/0/1997, please tell me what can I do

val c = Calendar.getInstance()
        c.add(Calendar.YEAR, -18)
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

        val abc = findViewById<Button>(R.id.birth)

        abc.setOnClickListener {

            val datePickerDialog = DatePickerDialog(
                this,
                DatePickerDialog.OnDateSetListener { view, year, month     , day ->
                    date.setText("" + day + "/" + month + "/" + year)
                },
                year,
                month,
                day
            )
            datePickerDialog.datePicker.maxDate = c.timeInMillis
            datePickerDialog.show()

        }
    }

回答1:


Mounts are starting from 0 to count. Type just;

val month = c.get(Calendar.MONTH) + 1



回答2:


Mounts are counted from zero (like array index):

  • January = 0
  • February = 1
  • ....

If you make an array that contains mount names in correct order the mount number given from calendar is the index of this array.

You can add one to it to get real mount.



来源:https://stackoverflow.com/questions/61198400/date-picker-shows-wrong-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!