Set date and get Date from Material Date Picker in Kotlin Android - android

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()
)

Related

How to get the days diffence between two date

How to calculate the days between two date for android studio in kotlin. There is two button for choosing date, after choosing the date, I want to compare two date and get the difference day between these two date.
Here is my code
tvDatePicker = findViewById(R.id.textViewDate)
btnDatePicker = findViewById(R.id.btn_datePicker)
EndDatePicker = findViewById(R.id.textViewBookEnd)
btnEndDatePicker = findViewById(R.id.btn_EndDatePicker)
val myCalendar = Calendar.getInstance()
val datePicker = DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
myCalendar.set(Calendar.YEAR,year)
myCalendar.set(Calendar.MONTH,month)
myCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)
updateStartDate(myCalendar)
}
val endDatePicker = DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
myCalendar.set(Calendar.YEAR,year)
myCalendar.set(Calendar.MONTH,month)
myCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)
updateEndDate(myEndCalendar)
}
btnDatePicker.setOnClickListener{
DatePickerDialog(this,datePicker, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show()
}
btnEndDatePicker.setOnClickListener{
DatePickerDialog(this,endDatePicker, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show()
}
private fun updateStartDate(myCalendar: Calendar){
val myFormat = "dd-MM-yyyy"
val sdf = SimpleDateFormat(myFormat, Locale.UK)
tvDatePicker.setText(sdf.format(myCalendar.time))
}
private fun updateEndDate(myCalendar: Calendar){
val myFormat = "dd-MM-yyyy"
val sdf = SimpleDateFormat(myFormat, Locale.UK)
EndDatePicker.setText(sdf.format(myCalendar.time))
}
private fun dayDifference(startCalendar: Calendar, endCalendar: Calendar): Long {
val diff = endCalendar.timeInMillis - startCalendar.timeInMillis
return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS).toInt()
}

How can I add months Int to a choosen date on date picker in android Kotlin?

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")

My app is closing after i pic the date , can anyone help me , i have attatched the code

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.

DatePicker dialog may showing improper date in kotlin

Below is the function I am using to diplay date picker dialog in android.
private fun openPurchaseDatePickerDialog(
yearToDisplay: Int,
monthToDisplay: Int,
dayToDisplay: Int
) {
try {
activity?.let { KeyboardUtils.hideKeyboard(it) }
val calendar = Calendar.getInstance()
val dialog = DatePickerDialog(activity, { _, year, month, day_of_month ->
calendar[Calendar.YEAR] = year
calendar[Calendar.MONTH] = month
calendar[Calendar.DAY_OF_MONTH] = day_of_month
val myFormat = "" + DateUtils.OverAllAppDateDisplayFormat
val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
edtPurchaseDate.setText(sdf.format(calendar.time).toString())
spIntendedUse.isFocusable = true
}, yearToDisplay, monthToDisplay, dayToDisplay)
dialog.updateDate(yearToDisplay,monthToDisplay,dayToDisplay)
dialog.datePicker.maxDate = calendar.timeInMillis
dialog.show()
} catch (e: Exception) {
e.printStackTrace()
}
As above you can check that there are three arguments passed in this function.
I have to show the specific date in DatePicker dialog show I have passed this three parameters.
Means If User selected the date first time the default values will be set or the current date will be set.
If edittext has already a text selected and not empty am doing as below :
if (edtPurchaseDate.text.toString().isNullOrEmpty()) {
val calendar = Calendar.getInstance()
openPurchaseDatePickerDialog(
calendar[Calendar.YEAR],
calendar[Calendar.MONTH],
calendar[Calendar.DAY_OF_MONTH]
)
} else {
var dateArr = edtPurchaseDate.text.toString().split("-")
openPurchaseDatePickerDialog(
dateArr[2].toInt(),
dateArr[1].toInt(),
dateArr[0].toInt()
)
}
But Still when the date picker dialogs opens its displaying the selected as as today instead of custom.
What might be the issue?
You can see I have also tried with updateDate() function as below :
dialog.updateDate(yearToDisplay,monthToDisplay,dayToDisplay)
Thanks.
Not completely sure about the updateDate method issue here . But to fix this you can use same Calendar object during initialization it should work fine .
i have modified your method a bit .
private fun openPurchaseDatePickerDialog(date: String) {
try {
val calendar = Calendar.getInstance()
if (date.isNotBlank()) {
try {
calendar.time = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).parse(date)!!
}catch (e:ParseException){
}
}
val dialog = DatePickerDialog(this, { _, year, month, day_of_month ->
calendar[Calendar.YEAR] = year
calendar[Calendar.MONTH] = month
calendar[Calendar.DAY_OF_MONTH] = day_of_month
val myFormat = "dd-MM-yyyy"
val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
edtPurchaseDate.setText(sdf.format(calendar.time).toString())
}, calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH])
dialog.datePicker.maxDate = System.currentTimeMillis()
dialog.show()
} catch (e: Exception) {
e.printStackTrace()
}
}
Now when you call it you just call it with a value you do mnot have to split String.
openPurchaseDatePickerDialog(edtPurchaseDate.text.toString())

How to get month from Material design Datepicker Android kotlin

I have a datepicker from material design, its opening well in the app.
Now how can i get for example the selected month from the datepicker.
Initialize the datepicker:
val datePicker = MaterialDatePicker.Builder.datePicker()
.setTitleText("Kies een datum")
.setSelection(MaterialDatePicker.todayInUtcMilliseconds())
.build()
On button hit the calendar opens:
fun pickDate() {
binding.DatepickerButton.setOnClickListener {
datePicker.show(activity?.supportFragmentManager!!, datePicker.toString())
}
}
so it shows, but i cannot get the data from it.
For Java 8, you can use Date/Time API
binding.DatepickerButton.addOnPositiveButtonClickListener {
var dateFormatter = DateTimeFormatter.ofPattern("MM", Locale.getDefault())
val month = dateFormatter.format(
Instant.ofEpochMilli(it)
.atZone(ZoneId.systemDefault()).toLocalDate()
)
}
Otherwise, you can use SimpleDateFormatter
binding.DatepickerButton.addOnPositiveButtonClickListener {
val calendar = Calendar.getInstance()
val simpleDateFormatter = SimpleDateFormat("MM", Locale.getDefault())
calendar.time = Date(it)
val month = simpleDateFormatter.format(calendar.time)
}
Try this click positive button click listener. after getting the date you can extract a month from it.
binding.DatepickerButton.addOnPositiveButtonClickListener {
val (startOfRange, endOfRange) = it // in case date range operation
val date = it // single select date
String dateString = DateFormat.format("dd/MM/yyyy", new Date(date)).toString();
}

Categories

Resources