2

The code below works well I am trying to figure how to calculate total number of days once the dates have been selected. Thank you so much.

The problem I'm trying to solve: Select vacation start date - date selector - this part works well Select vacation end date - date selector - this part works well Total days =Calculate End date - Start date - I don't know how to conduct the calculations Total 10 days

Here is my code

class VacationActivity : AppCompatActivity() {

    private var selectedYear = 0
    private var selectedMonth = 0
    private var selectedDay = 0

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vacation)


        // get view
        val tvDate = findViewById<TextView>(R.id.from_date_view)
        tvDate.setOnClickListener {

            val currentDate = Calendar.getInstance()
            val year = currentDate.get(Calendar.YEAR)
            val month = currentDate.get(Calendar.MONTH)
            val day = currentDate.get(Calendar.DAY_OF_MONTH)


            if (tvDate.text.isNotEmpty()) {
                this.selectedYear
                this.selectedMonth
                this.selectedDay
            }

            var listener = DatePickerDialog.OnDateSetListener { view, selectedYear, selectedMonth, selectedDay ->
                this.selectedYear = selectedYear
                this.selectedMonth = selectedMonth
                this.selectedDay = selectedDay

                tvDate.text = "${selectedMonth + 1}/$selectedDay/$selectedYear"

            }
            val datePicker = DatePickerDialog(this, listener, year, month, day)
            datePicker.show()
        }


        val tvDate2 = findViewById<TextView>(R.id.to_date_view)
        tvDate2.setOnClickListener {

            val currentDate = Calendar.getInstance()
            val year = currentDate.get(Calendar.YEAR)
            val month = currentDate.get(Calendar.MONTH)
            val day = currentDate.get(Calendar.DAY_OF_MONTH)


            if (tvDate2.text.isNotEmpty()) {
                this.selectedYear
                this.selectedMonth
                this.selectedDay
            }

            var listener = DatePickerDialog.OnDateSetListener { view, selectedYear, selectedMonth, selectedDay ->
                this.selectedYear = selectedYear
                this.selectedMonth = selectedMonth
                this.selectedDay = selectedDay

                tvDate2.text = "${selectedMonth + 1}/$selectedDay/$selectedYear"
            }

            val datePicker = DatePickerDialog(this, listener, year, month, day)
            datePicker.show()


        }


            }
        }
Simona Buga
  • 341
  • 6
  • 22
  • what kind of problem you faced in the code? – sasikumar Nov 13 '18 at 11:54
  • Please describe, what is the issue in your code. Is it working or not, if not then which line failing. – Akshay Paliwal Nov 13 '18 at 12:03
  • 1
    so far the code works well. The date pickers are displaying the information but I don't know how to do the calculation. Thank you so much – Simona Buga Nov 13 '18 at 12:32
  • Possible duplicate of [date difference in days, in Android](https://stackoverflow.com/questions/5405610/date-difference-in-days-in-android) (i know that that question doesn’t use Kotlin code, so you would have to translate). – Ole V.V. Nov 13 '18 at 13:12

1 Answers1

2

You can use these functions:

fun getLocalDateFromString(d: String, format: String): LocalDate {
    return LocalDate.parse(d, DateTimeFormatter.ofPattern(format))
}

it returns a LocalDate from a string d formatted with format.

fun getDaysDif(fromDate: LocalDate, toDate: LocalDate): Long {
    return ChronoUnit.DAYS.between(fromDate, toDate)
}

it returns the difference in days between 2 dates (LocalDate)

So you can do:

val days = getDaysDif(
        getLocalDateFromString(tvDate1.text, "MM/dd/yyyy"),
        getLocalDateFromString(tvDate2.text, "MM/dd/yyyy"))

The above code works for API level 26 and above.

For lower versions, use this:

val format = SimpleDateFormat("MM/dd/yyyy")
val days = TimeUnit.DAYS.convert(
    format.parse(tvDate2.text).getTime() - 
    format.parse(tvDate1.text).getTime(), 
    TimeUnit.MILLISECONDS)
forpas
  • 160,666
  • 10
  • 38
  • 76