-1

When selecting a date on DatePickerDialog the months gets 0. I used this same code past weeks. Date and Year are working fine. I searched on Internet and everything seems alike tutorials.

fun datePick (context: Context, textView: TextView){
    var calendar = Calendar.getInstance()

    var sday = calendar.get(android.icu.util.Calendar.DAY_OF_MONTH)
    var smonth = calendar.get(android.icu.util.Calendar.MONTH)
    var syear = calendar.get(android.icu.util.Calendar.YEAR)

    var listener =DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
        sday = dayOfMonth
        smonth = month
        syear = year
        textView.text = "$sday/$month/$syear"  }

    var dialog =DatePickerDialog(context,listener,syear,smonth,sday)
    dialog.show()
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    What value are you expecting? Do note that months are zero-based; i.e., January is 0, February is 1, etc. – Mike M. Jan 11 '19 at 23:47
  • Duplicate of [android calendar month begin at 0](https://stackoverflow.com/q/10529102/5221149) – Andreas Jan 11 '19 at 23:59

1 Answers1

0

In the Calendar class in Java, months are represented with a zero-based index (January is 0, February is 1.. and so on). This behavior is expected and you can easily adjust your code to blend with this behavior. For more information on why months are zero-based, read the answer to this question.

Note that ONLY MONTHS have this odd style of indexing. Days of the week, as well as the year, work normally as you'd expect.

I hope this helps.. Merry coding!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69