问题
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