Skip to content
Advertisement

How can I set current date (Today’s date) same as selected date in Material Date Picker Android

How can I set current date (Today’s date) to my local date. My timezone is GMT+07:00. I can just set only setSelection() date but can’t set current date. (Sorry for my any grammatical mistake and thank you in advance) Calendar image

  val c = Calendar.getInstance(TimeZone.getTimeZone("GMT+07:00"))
        val gmtTime: Long = c.getTime().getTime()
        c.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
        val timezoneThailand: Long = gmtTime + TimeZone.getTimeZone("Asia/Bangkok").rawOffset
        c.setTimeInMillis(timezoneThailand)
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

//        view.chooseDateButton.text = c.toString()
        val calendarConstraintBuilder = CalendarConstraints.Builder()
        calendarConstraintBuilder.setStart(1640995200000)
        calendarConstraintBuilder.setEnd(1646092800000)
        calendarConstraintBuilder.setValidator(DateValidatorPointBackward.now());

        val materialDateBuilder = MaterialDatePicker.Builder.datePicker()
        materialDateBuilder.setTitleText("SELECT DATE TO SHOW IMAGES");

        materialDateBuilder.setSelection(c.timeInMillis)

//        materialDateBuilder.setTheme(R.style.DefaultDatePickerTheme)

        materialDateBuilder.setCalendarConstraints(calendarConstraintBuilder.build())
        val materialDatePicker : MaterialDatePicker<*> = materialDateBuilder.build()

Advertisement

Answer

I could be wrong, but I think you don’t need to worry about timezone at all. It’s all handled automatically. Remove all code regarding to your Calendar c and just do the selection with

materialDateBuilder.setSelection(Calendar.getInstance().timeInMillis)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement