如何将当前日期(今天的日期)设置为与在 Material 日期选择器 Android 中选择的日期相同
How can I set current date (Today's date) same as selected date in Material Date Picker Android
如何将当前日期(今天的日期)设置为本地日期。我的时区是 GMT+07:00。我只能设置 setSelection() 日期但不能设置当前日期。
(对不起,我的任何语法错误,提前谢谢你)
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()
我可能是错的,但我认为您根本不需要担心时区问题。这一切都是自动处理的。删除与您的日历有关的所有代码 c
并使用
进行选择
materialDateBuilder.setSelection(Calendar.getInstance().timeInMillis)
如何将当前日期(今天的日期)设置为本地日期。我的时区是 GMT+07:00。我只能设置 setSelection() 日期但不能设置当前日期。 (对不起,我的任何语法错误,提前谢谢你) 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()
我可能是错的,但我认为您根本不需要担心时区问题。这一切都是自动处理的。删除与您的日历有关的所有代码 c
并使用
materialDateBuilder.setSelection(Calendar.getInstance().timeInMillis)