I am trying to find if selected date
and today's date
difference is equal to or greater than 18 years or not.
For getting today's date following is my code
private val simpleDateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
private val date = Date()
private val todayDate = simpleDateFormat.parse(simpleDateFormat.format(date)).time
private val cal = Calendar.getInstance()
cal.time = date
val date = simpleDateFormat.parse("12-11-2001").time
val years = (todayDate - date) / (1000L * 60 * 60 * 24 * cal.getActualMaximum(Calendar.DAY_OF_YEAR))
Now my selected date is 12-11-2001 and todays date is 11-11-2019 so it should show 17 years but it show 18 years. Even if I select 13,14 or 15th November 2001, it shows 18 years instead of 17 years but if I select 16th Novemeber 2001 then it shows 17 years. I am not able to understand why this behaviour happen.