DatePicker in Kotlin showing wrong month

Related searches

When implementing Datepicker in kotlin instead of July its showing M07 in all devices.

Here is code for Date Picker:

val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> //differnce(dayOfMonth, monthOfYear, year, false) }, year, month, day) datePickerDialog.show()

try this:

class MainActivity : AppCompatActivity() {

var button_date: Button? = null
var textview_date: TextView? = null
var cal = Calendar.getInstance()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // get the references from layout file
    textview_date = this.text_view_date_1
    button_date = this.button_date_1

    textview_date!!.text = "--/--/----"

    // create an OnDateSetListener
    val dateSetListener = object : DatePickerDialog.OnDateSetListener {
        override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
                               dayOfMonth: Int) {
            cal.set(Calendar.YEAR, year)
            cal.set(Calendar.MONTH, monthOfYear)
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            updateDateInView()
        }
    }

    // when you click on the button, show DatePickerDialog that is set with OnDateSetListener
    button_date!!.setOnClickListener(object : View.OnClickListener {
        override fun onClick(view: View) {
            DatePickerDialog(this@MainActivity,
                    dateSetListener,
                    // set DatePickerDialog to point to today's date when it loads up
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH)).show()
        }

    })
}

private fun updateDateInView() {
    val myFormat = "MM/dd/yyyy" // mention the format you need
    val sdf = SimpleDateFormat(myFormat, Locale.US)
    textview_date!!.text = sdf.format(cal.getTime())
}

show calendar like this please try

Android: DatePicker shows wrong month � Issue #4851 , Tell us about the problem For 9th and 10th months DatePicker shows October. Which platform(s) does your issue occur on? Android Please� We can also show the DatePicker in spinner format like selecting the day, month and year separately, by using android:datePickerMode attribute and set android:calendarViewShown=”false”, otherwise both spinner and calender can be seen simultaneously.


Try replacing

{ view, year, monthOfYear, dayOfMonth -> //differnce(dayOfMonth, monthOfYear, year, false) }

With

{ view, year, monthOfYear, dayOfMonth -> this.year = year this.month = monthOfYear this.dayOfMonth = dayOfMonth}

DatePicker in Kotlin showing wrong month, When implementing Datepicker in kotlin instead of July its showing M07 in all devices. enter image description here. Here is code for Date Picker: Android DatePicker is a user interface control which is used to select the date by day, month and year in our android application. DatePicker is used to ensure that the users will select a valid date. In android DatePicker having two modes, first one to show the complete calendar and second one shows the dates in spinner view.


datebtn.setOnClickListener {
            val c= Calendar.getInstance()
            val year= c.get(Calendar.YEAR)
            val month = c.get(Calendar.MONTH)
            val day = c.get(Calendar.DAY_OF_MONTH)
            var dpd = DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, mYear,mMonth , mDay ->
                val mmMonth = mMonth+1
                val date = "$mDay/$mmMonth/$mYear"
                datetv.setText(date)
            },year,month,day)
            dpd.show()
        }

DatePicker in Kotlin, We can also show the DatePicker in spinner format like selecting the day, month and year separately, by using android:datePickerMode attribute� Overview Android Date Picker Dialog Complete example and code of Android DatePickerDialog in kotlin . Let’s Build Android Android Date Picker Dialog example in kotlin: In the kotlin showing DatePickerDialog is very easy, in this example see how much easy. In the example the selected data will show in Toast.


DatePicker, Kotlin |Java DatePicker_datePickerMode attribute is set to calendar , the month and day can be selected using a calendar-style view android:minDate, The minimal date shown by this calendar view in mm/dd/yyyy format. Creating a simple Android Date Picker app extending DatePickerDialog class using Kotlin. Learn about how to use Calendar instance; Note: In this post, I used Android Studio 3.5.3, make sure you use the latest Android Studio, or if you already install it, be sure to check the latest update. The Kotlin version that I used is Kotlin 1.3.61.


Date picker is showing wrong dates on Android app., Hi, My calendar looks funny on the mobile app. It is mismatching dates with the weekdays for the current month. It is showing, for example, that� Android DatePicker – Kotlin Example. Android DatePicker – Kotlin Example: To pick a date from a DatePicker using DatePickerDialog, Create an Android Application with Kotlin Support and replace activity_main.xml and MainActivity.kt with the following content. And then we shall see an explanation about the code.


Kotlin™ is protected under the Kotlin Foundation and licensed under the Apache 2 license. Supported and developed by JetBrains Supported and developed by JetBrains