I have a requirements where I need to limit the allowed date in DatePicker from year 2009 up to current date only. Meaning the supported date for example will be from Jan 1, 2009 up to current date only.
The current implementation we had with the old DatePickerDialog
val calendar = Calendar.getInstance()
val year = calendar[Calendar.YEAR]
val month = calendar[Calendar.MONTH]
val day = calendar[Calendar.DAY_OF_MONTH]
val datePickerDialog = DatePickerDialog(appContext,
R.style.AppDatePicker,
dateSetListener,
year,
month,
day)
//Oldest date will be 2009
calendar.add(Calendar.YEAR, 2009 - year)
datePickerDialog.datePicker.minDate = calendar.timeInMillis
//Latest date will be the current date
datePickerDialog.datePicker.maxDate = System.currentTimeMillis()
// datePickerDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
//Pop up the DatePicker dialog
datePickerDialog.show()
Additional possible improvement is to limit the supported date by specifying the date statically.
Something like
val startDate = "01/01/2009"
val endDate = "03/27/2022"
calendarPicker.minDate = Date(startDate)
calendarPicker.maxDate = Date(endDate)
Currently looking on CalendarConstraints.DateValidator and CalendarConstraints.Builder() but do not know how to work with it base on my requirements.
I don't know if you still need it, but maybe it will help others too.
I had a similar problem where I needed only dates in the range from the previous day to 45 days behind the current date to be enabled. That is, today, January 18th, the calendar would only be enabled from 12-05-2022 to 01-17-2023.
I did it like this:
val dateValidatorMin: DateValidator =
DateValidatorPointForward.from(
Calendar.getInstance().timeInMillis - 45.days.toLong(DurationUnit.MILLISECONDS))
val dateValidatorMax: DateValidator =
DateValidatorPointBackward.before(
Calendar.getInstance().timeInMillis - 1.days.toLong(DurationUnit.MILLISECONDS))
enter code here
val dateValidator: DateValidator = CompositeDateValidator.allOf(listOf(dateValidatorMin, dateValidatorMax))
val constraints: CalendarConstraints =
CalendarConstraints.Builder()
.setValidator(dateValidator)
.build()
val builder = MaterialDatePicker.Builder.dateRangePicker()
.setCalendarConstraints(constraints)
.setTitleText(getString(R.string.label_select_date_range))
val picker = builder.build()
And the result was like this:
Hope this helps.
Related
I need to get this week startdate like 2023-02-20 and last week startdate and end date.
startdate will be monday.
so I create 4 variables like below.
private var thisWeekStart : Long = 0
private var thisWeekEnd : Long = 0
private var lastWeekStart : Long = 0
private var lastWeekEnd : Long = 0
And I tried to assign something like below..
var cal = Calendar.getInstance()
cal.time = Date()
thisWeekEnd = cal.timeInMillis
// 이번주 시작
cal.get(Calendar.DAY_OF_WEEK)
thisWeekStart = cal.timeInMillis
cal.add(Calendar.DAY_OF_WEEK, -1)
lastWeekStart = cal.timeInMillis
cal.clear()
cal.set(Calendar.DAY_OF_WEEK, -1)
lastWeekStart = cal.timeInMillis
But it throws milliseconds not like yyyy-MM-dd format.
And I'm not sure is that correct way of keep clearing calendar like above.
Most of all, I can't get last week's end date with above way.
Is there any good way to get this weed and last week start, end date?
At first, we define our desired output format. In this case we will use yyyy-MM-dd. In the next step we save the current date in a variable. In the third line we define timezone in which we are working (I used Europe/Berlin for testing purposes, bacause I'm in germany).
In the next steps we create Calendar instance for each desired date and manipulate the date to our needs.
Important: You have to add the line firstDayOfWeek = Calendar.MONDAY only if your week starts at another date than Sunday.
val dateFormat = SimpleDateFormat("yyyy-MM-dd")
val today = Calendar.getInstance()
val timeZone = TimeZone.getTimeZone("Europe/Berlin")
val startOfWeek = Calendar.getInstance(timeZone).apply {
firstDayOfWeek = Calendar.MONDAY
set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
}.time
val endOfWeek = Calendar.getInstance(timeZone).apply {
firstDayOfWeek = Calendar.MONDAY
set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
}.time
val startOfLastWeek = Calendar.getInstance(timeZone).apply {
firstDayOfWeek = Calendar.MONDAY
set(Calendar.WEEK_OF_YEAR, today.get(Calendar.WEEK_OF_YEAR) - 1)
set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
}.time
val endOfLastWeek = Calendar.getInstance(timeZone).apply {
firstDayOfWeek = Calendar.MONDAY
set(Calendar.WEEK_OF_YEAR, today.get(Calendar.WEEK_OF_YEAR) - 1)
set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
}.time
val startOfWeekAsString = dateFormat.format(startOfWeek)
val endOfWeekAsString = dateFormat.format(endOfWeek)
val startOfLastWeekAsString = dateFormat.format(startOfLastWeek)
val endOfLastWeekAsString = dateFormat.format(endOfLastWeek)
println("Start of week: $startOfWeekAsString")
println("End of week: $endOfWeekAsString")
println("Start of last week: $startOfLastWeekAsString")
println("End of last week: $endOfLastWeekAsString")
Expected output:
Start of week: 2023-02-13
End of week: 2023-02-19
Start of last week: 2023-02-06
End of last week: 2023-02-12
fun getStartAndEndDayByToday(today: Calendar): ArrayList<String> {
val startAndEndDayArray = ArrayList<String>()
// Get the day of this week today
val todayWeekDay = today.get(Calendar.DAY_OF_WEEK)
// Below you can get the start day of week; it would be changeable according to local time zone
today.add(Calendar.DAY_OF_MONTH, 1 - todayWeekDay)
val calStartDayOfWeek = today.time
// Below you can get the end day of week; it would be changeable according to local time zone
today.add(Calendar.DAY_OF_MONTH, 6)
val calEndDayOfWeek = today.time
// Here you can set the date format by using SimpleDateFormat
val sdfStartDayOfWeek = SimpleDateFormat("yyyy-MM-dd")
val sdfEndDayOfWeek = SimpleDateFormat("yyyy-MM-dd")
// Make the array with start date and end date
startAndEndDayArray.add(sdfStartDayOfWeek.format(calStartDayOfWeek))
startAndEndDayArray.add(sdfEndDayOfWeek.format(calEndDayOfWeek))
return startAndEndDayArray
}
Hope this would help!
I have a working date picker in my app that replaces an EditText after a date selection. I want to add duration through a RadioGroup button that prints an Int to provoke an end date. How can I do that? I've spent the last two days without getting the result I'm looking to get.
Here is what I've got so far.
val datePicker = findViewById<DatePicker>(R.id.date_Picker)
val today = Calendar.getInstance()
datePicker.init(
today.get(Calendar.YEAR),
today.get(Calendar.MONTH),
today.get(Calendar.DAY_OF_MONTH
) { view, year, month, day ->
val month = month + 1
val startDay = ("$day-$month-$year")
binding.fechadeinicio.text = fechainicio
val duration = when (binding.duracion.checkedRadioButtonId) {
R.id.doce -> 12
R.id.veinticuatro -> 24
R.id.treintayseis -> 36
else -> 36
}
// val endDate = startDate.plusMonths(Duration.toLong())
// binding.endDate.text = endDate.toString()
}
Here is the closest one to the result I'm looking to get. Yet, I want to use the selected date val startDay, instead of val date = LocalDate.parse("2020-05-03"). When I replace it, the app crashes.
val date = LocalDate.parse("2020-05-03")
// Displaying date
println("Date : $date")
// Add 2 months to the date
val newDate = date.plusMonths(2)
println("New Date : $newDate")
Please, let me know how I can get the desired result?
Thanks.
I want to use the selected date that is val startDay instead of val date = LocalDate.parse("2020-05-03"). When I replace it, the app crashes.
val startDay = ("$day-$month-$year") here you've created date as dd-MM-yyyy, but by default LocalDate.parse uses DateTimeFormatter.ISO_LOCAL_DATE to parse a string, that parses a string of format yyyy-MM-dd to LocalDate. That's why it's crashing as your date is invalid according to that format.
You have to provide a DateTimeFormatter of pattern dd-MM-yyyy to parse your date.
You can do it like this
val startDay = ("$day-$month-$year")
val dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
binding.fechadeinicio.text = fechainicio
val duration = when (binding.duracion.checkedRadioButtonId) {
R.id.doce -> 12
R.id.veinticuatro -> 24
R.id.treintayseis -> 36
else -> 36
}
val startDate = LocalDate.parse(startDay, dateFormatter)
val endDate = startDate.plusMonths(duration.toLong()).format(dateFormatter)
binding.endDate.text = endDate
DatePicker returns year, month and day int values, now on creating date like val startDay = ("$day-$month-$year") would result in single digit for days and months less than 10, which would return 1 Jan 2020 as 1-1-2020 but date formatter is expecting it to be 01-01-2020.
To deal with this, we've to format int values before assigning it to the startDay, we can use format method of String to return 2 digits like this "%02d".format(intValue)
Change
val startDay = ("$day-$month-$year")
to
val startDay = "${"%02d".format(day)}-${"%02d".format(month)}-$year"
You can use LocalDate using of function.
Example here:
val date = LocalDate.of(year, month, day)
// Displaying date
println("Date : $date")
// Add 2 months to the date
val newDate = date.plusMonths(2)
println("New Date : $newDate")
I'm learning kotlin and at the moment I don't know much, I want to change a datepicker that I have for one of type Material, the problem is that I don't know how to pass the data to this new date picker.
This is the one I have at the moment:
fecha = view.findViewById(R.id.fecha)
fecha?.setOnClickListener {
fecha!!.error = null
val dateSetListener = DatePickerDialog(requireContext(), { _, year, monthOfYear, dayOfMonth ->
cal.set(Calendar.YEAR, year)
mYear = year
cal.set(Calendar.MONTH, monthOfYear)
mMonth = monthOfYear
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
mDay = dayOfMonth
updateDateInView()
}, mYear, mMonth, mDay)
dateSetListener.datePicker.maxDate = System.currentTimeMillis()
dateSetListener.show()
}
fun updateDateInView() {
val myFormat = "dd/MM/yyyy"
val sdf = SimpleDateFormat(myFormat)
fecha?.setText(sdf.format(cal.time))
}
I want to make it like this but I don't know how to pass and save the values, could someone help me?
val datePicker = MaterialDatePicker.Builder.datePicker().build()
MaterialDatePicker accepts CalendarConstraints to open the date picker on a certain month. CalendarConstraints accepts timeInMilliseconds to open calendar on a particular month. MaterialDatePicker has an addOnPositiveButtonClickListener method whose lambda returns the time in milliseconds after the user makes a selection.
You can create MaterialDatePicker like this
val myFormat = "dd/MM/yyyy"
val formattedDate = "01/01/2000"
val sdf = SimpleDateFormat(myFormat)
val date = sdf.parse(formattedDate)
val timeInMillis = date.time
val constraintBuilder = CalendarConstraints.Builder().setOpenAt(
timeInMillis //pass time in milli seconds
).build()
val picker = MaterialDatePicker.Builder.datePicker()
.setTitleText("Select Date")
.setCalendarConstraints(constraintBuilder)
.build()
picker.addOnPositiveButtonClickListener {
val date = Date(it)
val formattedDate = sdf.format(date) // date selected by the user
}
// show picker using this
picker.show(requireActivity().supportFragmentManager, "materialDatePicker")
Instead of using SimpleDateFormatter, you should use LocalDateTime API provided in Java8
Using LocalDateTime API, you can do the same like this
val dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.getDefault())
val formattedDate = "01/01/2000"
val timeInMillis = LocalDate.parse(formattedDate, dateFormatter)
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli()
You can pass this timeInMillis to setOpenAt() method of CalendarConstraintSet.
To get the date after the user makes a selection
val timeInMillis = dateFormatter.format(
// it is the milliseconds received inside lambda of addOnPositiveButtonClickListener
Instant.ofEpochMilli(it)
.atZone(ZoneId.systemDefault()).toLocalDate()
)
This is my code and it crashes after i select the date in date picker dialog
bdaybutton.setOnClickListener { view -\\\>
val c = Calendar.getInstance()
val cyear = c.get(Calendar.YEAR)
val cmonth = c.get(Calendar.MONTH)
val cday = c.get(Calendar.DAY_OF_MONTH)
val abc = DatePickerDialog( this , DatePickerDialog.OnDateSetListener
{ view , year, month, day -\\\>
flag2 = true
year2 = year
val selecteddate = "${day/month+1/year}"
bdaydatetext.text = "${day/month+1/year}"
val sdf = java.text.SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
val date2 = sdf.parse(selecteddate)
time2 = date2.time.toInt()
}, cyear, cmonth, cday)
abc.show()
]
}
The error in the imge appears when after selecting the date
"${day/month+1/year}"
This computes the expression inside the {} and converts it to a string. Looks like you wanted something like
"${day}/${month+1}/${year}"
instead there.
Better yet, skip the string conversion step altogether and use something like Java 8 LocalDate or LocalDateTime directly instead.
i'm trying to get te selected date from DatePicker but always i have one day less than the selected
For example, if I select 14/2/2022 i obtain 13/2/2022 and if I select 8/10/2018 i obtain 7/10/2018
This is my code:
private fun DatePickerSelected() {
val picker = MaterialDatePicker.Builder.datePicker()
.setTitleText("Select date of birth")
.setSelection(MaterialDatePicker.todayInUtcMilliseconds())
.build()
picker.addOnPositiveButtonClickListener {
val date = Date(picker.selection!!)
Log.d("Date",date.toString())
val dateString = SimpleDateFormat("dd/MM/yyyy").format(date)
binding.edtBirthday.editText?.setText(dateString)
}
picker.show(requireActivity().supportFragmentManager, "BirthdayPicker")
}
Which is the problem? Thanks!
I found the solution using calendar
picker.addOnPositiveButtonClickListener {
val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
utc.timeInMillis = it
//+1: The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0
val stringData = "${utc.get(Calendar.DAY_OF_MONTH)}/${utc.get(Calendar.MONTH)+1}/${utc.get(Calendar.YEAR)}"
binding.edtBirthday.editText?.setText(stringData)
}
try this code, add one day
date.add(Calendar.DAY_OF_MONTH,1)