-2

I want to calculate the difference between the date selected by the user and the current date

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)

    var year = intent.getStringExtra("year")
    var month = intent.getStringExtra("month")
    var day = intent.getStringExtra("day")

    Log.d("select_day", "${year}.${month}.${day}")

    var selectday = Calendar.getInstance()
    selectday.set(year.toInt(), month.toInt(), day.toInt())

    var today = Calendar.getInstance()
    var yearto = today.get(Calendar.YEAR)
    var monthto = today.get(Calendar.MONTH)
    var dayto = today.get(Calendar.DAY_OF_MONTH)
    today.set(yearto, monthto+1, dayto)
    var todaym = today.timeInMillis / 86400000
    var selectdaym = selectday.timeInMillis / 86400000
    var deltil = selectdaym - todaym
    testtv.setText("${deltil.toInt()}")

}

For example, if the value passed to intent is year 2019 month 9 day 1 and yearto 2019 monthto 8 dayto 28, the final result is 3 instead of 4.

Zoe
  • 27,060
  • 21
  • 118
  • 148
박주현
  • 1
  • 2

1 Answers1

0

You could take the variable named as a string to store your date and after then you subtract it, then convert it to date again.

Zakiy
  • 21
  • 3