Home > other >  how to find the difference between datepicker in kotlin
how to find the difference between datepicker in kotlin

Time:03-27

Currently, I have a start date and a end date for my datepicker dialog. I am trying to find the difference in days between the start and the end date. And to make sure that the end date will not be is not on the earlier date

/* Calendar Start Date Button Click */
        startdatepicker = findViewById(R.id.CalendarStartImage)
        startdateview = findViewById(R.id.StartDateTextView)
        val startCalendar = Calendar.getInstance()

        val startPicker = DatePickerDialog.OnDateSetListener{ view, year, month,dayOfMonth ->
            startCalendar.set(Calendar.YEAR,year)
            startCalendar.set(Calendar.MONTH,month)
            startCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)
            updateStartCalendar(startCalendar)
        }

        startdatepicker.setOnClickListener{
            val dialog = DatePickerDialog(this,startPicker, startCalendar.get(Calendar.YEAR), startCalendar.get(Calendar.MONTH), startCalendar.get(Calendar.DAY_OF_MONTH))
//            dialog.datePicker.minDate = startCalendar.getTimeInMillis()
            dialog.datePicker.minDate = (System.currentTimeMillis()-1000)
            dialog.show()
        }
        /* Calendar End Date Button Click */
        enddatepicker = findViewById(R.id.CalendarEndImage)
        enddateview = findViewById(R.id.EndDateTextView)
        val endCalendar = Calendar.getInstance()
        val endPicker = DatePickerDialog.OnDateSetListener{ view, year, month,dayOfMonth ->
            endCalendar.set(Calendar.YEAR,year)
            endCalendar.set(Calendar.MONTH,month)
            endCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)
//            DatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            updateEndCalendar(endCalendar)
        }

        enddatepicker.setOnClickListener{
            val dialog = DatePickerDialog(this,
                endPicker,
                endCalendar.get(Calendar.YEAR),
                endCalendar.get(Calendar.MONTH),
                endCalendar.get(Calendar.DAY_OF_MONTH))
//            dialog.datePicker.minDate = startCalendar.getTimeInMillis()
            dialog.datePicker.minDate = (System.currentTimeMillis()-1000)
            dialog.show()
        }

    }
    private fun updateStartCalendar(startCalendar: Calendar){
        val myformat = "MM-dd-yyyy"
        val mydateformat = SimpleDateFormat(myformat,Locale.ENGLISH)
        startdateview.text=(mydateformat.format(startCalendar.time))
    }
    private fun updateEndCalendar(endCalendar: Calendar){
        val myformat = "MM-dd-yyyy"
        val mydateformat = SimpleDateFormat(myformat,Locale.ENGLISH)
        Log.d("check", endCalendar.time.toString())
        enddateview.text=(mydateformat.format(endCalendar.time))
    }

CodePudding user response:

to check the time difference between two calendar dates you can use:

val duration = Duration.between(startCalendar, endCalendar)
val durationDays = duration.inWholeDays()

you can also check if it's negative using:

if(duration.isNegative){
    //do something
} else{
    //do something else
}
  • Related