I want to get age difference from date picker dialogs in Android but I don't know how to get difference in age from two datepicker dialogs and then set them in years,months & days.
Please see image below to get idea What I want to do....
package com.example.codingtutorials
import android.app.DatePickerDialog
import android.media.Image
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import java.lang.String
import java.text.SimpleDateFormat
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.Period
import java.util.*
import java.util.concurrent.TimeUnit
class ActivityAgeCalculator : AppCompatActivity() {
private lateinit var iv_selectDate: ImageView
private lateinit var iv_select_birth_year: ImageView
private lateinit var btn_clear: Button
private lateinit var btn_ageCalculate: Button
private lateinit var et_day: EditText
private lateinit var tday: EditText
private lateinit var et_month: EditText
private lateinit var tmonth: EditText
private lateinit var et_year: EditText
private lateinit var tyear: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dob)
iv_selectDate = findViewById(R.id.select_date)
iv_select_birth_year = findViewById(R.id.select_birth_year)
val tv_day = findViewById<TextView>(R.id.tv_day)
//birth et
et_day = findViewById(R.id.et_day)
et_month = findViewById(R.id.et_month)
et_year = findViewById(R.id.et_year)
//today et
tday = findViewById(R.id.day)
tmonth = findViewById(R.id.month)
tyear = findViewById(R.id.year)
val myCalendar = Calendar.getInstance()
val year = myCalendar.get(Calendar.YEAR)
val month = myCalendar.get(Calendar.MONTH)
val day = myCalendar.get(Calendar.DAY_OF_MONTH)
tday.setText(String.valueOf(day))
tmonth.setText(String.valueOf(month + 1))
tyear.setText(String.valueOf(year))
btn_clear = findViewById(R.id.btn_clear)
btn_ageCalculate = findViewById(R.id.btn_calculate)
btn_clear.setOnClickListener(View.OnClickListener {
et_day.setText("")
et_month.setText("")
et_year.setText("")
})
iv_selectDate.setOnClickListener(View.OnClickListener {
clickDatePick()
})
iv_select_birth_year.setOnClickListener(View.OnClickListener {
clickDatePicker()
})
btn_ageCalculate.setOnClickListener(View.OnClickListener {
})
}
fun clickDatePicker() {
val birth = Calendar.getInstance()
val year = birth.get(Calendar.YEAR)
val month = birth.get(Calendar.MONTH)
val day = birth.get(Calendar.DAY_OF_MONTH)
val dpd_birth = DatePickerDialog(
this,
{ _, year, month, dayofMonth ->
val birthdate = "$dayofMonth/${month + 1}/$year"
val mday = (String.valueOf(dayofMonth))
val mmonth = (String.valueOf(month + 1))
val myear = (String.valueOf(year))
et_day.setText(mday)
et_month.setText(mmonth)
et_year.setText(myear)
val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
val theDate = sdf.parse(birthdate)
theDate?.let {
val selectedDateInMInutes = theDate.time / 60000
val currentDate = sdf.parse(sdf.format(System.currentTimeMillis()))
currentDate?.let {
val currentDateInMinutes = currentDate.time / 60000
val differenceInMinutes = currentDateInMinutes - selectedDateInMInutes
/* // =============Difference between Dates================//
val diff: Long = currentDateInMinutes - selectedDateInMInutes
val seconds = diff / 1000
val minutes = seconds / 60
val hours = minutes / 60
val days = hours / 24
val toDays = TimeUnit.MILLISECONDS.toDays(diff)
val tohours = TimeUnit.MILLISECONDS.toHours(diff)
val tominutes = TimeUnit.MILLISECONDS.toMinutes(diff)
val toseconds = TimeUnit.MILLISECONDS.toSeconds(diff)
// =============Difference between Dates================//
*/
}
}
},
year,
month,
day,
)
dpd_birth.datePicker.maxDate = System.currentTimeMillis()
dpd_birth.show()
}
private fun clickDatePick() {
val today = Calendar.getInstance()
val year = today.get(Calendar.YEAR)
val month = today.get(Calendar.MONTH)
val day = today.get(Calendar.DAY_OF_MONTH)
val dpd_today = DatePickerDialog(
this,
{ _, year, month, dayofMonth ->
val selectedDate = "$dayofMonth/${month + 1}/$year"
val mday = (String.valueOf(dayofMonth))
val mmonth = (String.valueOf(month + 1))
val myear = (String.valueOf(year))
tday.setText(mday)
tmonth.setText(mmonth)
tyear.setText(myear)
val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
val theDate = sdf.parse(selectedDate)
theDate?.let {
val selectedDateInMInutes = theDate.time / 60000
val currentDate = sdf.parse(sdf.format(System.currentTimeMillis()))
currentDate?.let {
val currentDateInMinutes = currentDate.time / 60000
val differenceInMinutes = currentDateInMinutes - selectedDateInMInutes
}
}
},
year,
month,
day,
)
dpd_today.datePicker.maxDate = System.currentTimeMillis()
dpd_today.show()
}
}