Mencari selisih hari dari 2 textview yang berisi datepicker kotlin

<a href=' https://drive.google.com/open?id=1f77oQu8ImqbWIj4Nr-YZezOGAkPuBbhD '> https://drive.google.com/open?id=1f77oQu8ImqbWIj4Nr-YZezOGAkPuBbhD </a>

class cartActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cart)
        supportActionBar!!.title = "Reservation"
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        txtmotorname.setText(intent.getStringExtra("nama"))
        txtprice.setText(intent.getStringExtra("harga"))
        date()

    }
    private fun date(){
        txtstartdate.setOnClickListener {
            val now = Calendar.getInstance()
            val datepicker = DatePickerDialog(
                this, DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                    txtstartdate.setText("" + dayOfMonth + "/" + month + "/" + year)
                },
                now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)
            )
            datepicker.show()
        }

        txtreturndate.setOnClickListener {
            val finish = Calendar.getInstance()
            val datepicker2 = DatePickerDialog(
                this, DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                    txtreturndate.setText("" + dayOfMonth + "/" + month + "/" + year)
                },
                finish.get(Calendar.YEAR), finish.get(Calendar.MONTH), finish.get(Calendar.DAY_OF_MONTH)
            )
            datepicker2.show()
        }

        btncheckout.setOnClickListener {
            val intent = Intent(this, DetailBookActivity::class.java)
            intent.putExtra("namaM", txtmotorname.text)
            intent.putExtra("mulai",txtstartdate.text)
            intent.putExtra("selesai",txtreturndate.text)
            startActivity(intent)

        }

    }

}

Bagamimana Cara mencari selisihnya hari dari 2 textView txtstartdate dan txtreturndate

avatar agusg
@agusg

7 Kontribusi 4 Poin

Diperbarui 5 tahun yang lalu

2 Jawaban:

Jawaban Terpilih

<pre> class cartActivity : AppCompatActivity() {

private val startDate = Calendar.getInstance()
private val returnDate = Calendar.getInstance()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_cart)
    supportActionBar!!.title = &quot;Reservation&quot;
    supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    txtmotorname.setText(intent.getStringExtra(&quot;nama&quot;))
    txtprice.setText(intent.getStringExtra(&quot;harga&quot;))
    date()


}
private fun date(){
    txtstartdate.setOnClickListener {
        getDatePicker(txtstartdate, startDate) 
    }

    txtreturndate.setOnClickListener {
        getDatePicker(txtreturndate, returnDate)
    }
    
    btncheckout.setOnClickListener {
        val intent = Intent(this, DetailBookActivity::class.java)
        intent.putExtra(&quot;namaM&quot;, txtmotorname.text)
        intent.putExtra(&quot;mulai&quot;,txtstartdate.text)
        intent.putExtra(&quot;selesai&quot;,txtreturndate.text)
        startActivity(intent)
    }

}

private fun getDatePicker(txtDate: TextView, calender: Calendar) {
    val now = Calendar.getInstance()
    val datePicker = DatePickerDialog(
            this, DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth -&amp;gt;
        val stringDate = &quot;$dayOfMonth/${month+1}/$year&quot;
        txtDate.text = stringDate
        calender.set(year, month, dayOfMonth)
        
        // txt_days_of_rent sesuaikan dengan ID Days Of Rent mu
        txt_days_of_rent.text = differentDays().toString()

    }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)
    )
    datePicker.show()
}

private fun differentDays(): Long {
    val diff = returnDate.timeInMillis - startDate.timeInMillis
    val diffDays = diff / (24 * 60 * 60 * 1000)
    return diffDays
}

} </pre>

avatar IfanZalukhu
@IfanZalukhu

217 Kontribusi 158 Poin

Dipost 5 tahun yang lalu

terimakasih mas berhasil, sy nyari ini 2 hari gk nemu2

avatar agusg
@agusg

7 Kontribusi 4 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban