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