0

I've a dates pickers, one for start date and one for end date, and my dates looks like this as string:

Start 2020-05-06

End 2020-05-08

My problem is, How can I calculate every time the differences?

For example, if the start date is 14/05, the end date can't be before 14/05.

Currently i've this check:

val from= calendar.clone() 

if (isStarted) {
            from.add(Calendar.DATE, -29)
        } else {
            val dayOfMonth: Int = extractDay(selectedStartDate)
            from.add(Calendar.DATE, ??) //the problem, set the minimum date to start date
        }

datePicker.minDate = from.timeInMillis

//Returning the day of startDate
      private fun extractDay(selectedStartDate: String?): Int {
            return selectedStartDate?.substring(selectedStartDate.length - 2)!!.toInt()
        }

But there is 2 problems.

1) How can I calculate the differences?

2) If for example user picks start and end date, and then pick start again it could be that the start date will be after the end date, how can I prevent this?

UPDATE: Question 2 answered in the comments

Andrey Molochko
  • 727
  • 1
  • 6
  • 21
Noam
  • 485
  • 1
  • 7
  • 18
  • 1
    Instead of preventing it, I suggest you show a warning to user. You can set min or max dates on date pickers, but sometimes that'll block user from choosing the start and end date in order. – Nicolas May 14 '20 at 13:50
  • Otherwise look at: [How to get milliseconds from a date picker?](https://stackoverflow.com/questions/11430358/how-to-get-milliseconds-from-a-date-picker). And set the calendar time of the day to 00:00:00.000 so both values will be the same if date is on the same day. You can then use that for comparison. – Nicolas May 14 '20 at 13:53
  • Your suggestion is actually a better idea, I'll remove this question from my post, thank you – Noam May 14 '20 at 13:56
  • I dont know Kotlin but in java lang is this. check [this](https://stackoverflow.com/questions/21285161/android-difference-between-two-dates) out. – tohid noori May 14 '20 at 15:10

2 Answers2

1

1) For calculating difference you should convert endTime to milliseconds and startTime to milliseconds. And after it you should take the second away from the first.

override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        val calendar = Calendar.getInstance()
        calendar.set(year, month, dayOfMonth)
        setNavigationResult(args.key, calendar.timeInMillis)
    }

This code you can use if you using navigation component, otherwise you can get milliseconds in onActivityResult, after closing a DatePicker.

2) For solving this problem you can compare startDate and endDate after every updating it. It's a normal practice. For it you should add method with two long parameters (start and end date in milliseconds). After it you can compare two vars and if end date will be less then start date you can show error and block further actions.

Andrey Molochko
  • 727
  • 1
  • 6
  • 21
-1

I suggest you test with Calendar.after() or Calendar.before()