Postingan lainnya
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
0
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 = "Reservation"
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
txtmotorname.setText(intent.getStringExtra("nama"))
txtprice.setText(intent.getStringExtra("harga"))
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("namaM", txtmotorname.text)
intent.putExtra("mulai",txtstartdate.text)
intent.putExtra("selesai",txtreturndate.text)
startActivity(intent)
}
}
private fun getDatePicker(txtDate: TextView, calender: Calendar) {
val now = Calendar.getInstance()
val datePicker = DatePickerDialog(
this, DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth -&gt;
val stringDate = "$dayOfMonth/${month+1}/$year"
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>
2
terimakasih mas berhasil, sy nyari ini 2 hari gk nemu2
1