简体   繁体   中英

Check user is greater than 18 years from todays date with date picker android

I am trying to check that my user should be 18 years old. If Not then show a toast. (Trying to get from exact today date). But result is getting success only year wise.

Output - Years are getting calculated. Expected - From today's date, user should be 18 years old.

this is what i have tried.

    val calendar = Calendar.getInstance()
    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_MONTH)
    val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            calendar.set(Calendar.YEAR, year)
            calendar.set(Calendar.MONTH, monthOfYear)
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            val sdf = SimpleDateFormat(myFormat, Locale.US)
            val dob = sdf.format(calendar.time)

            val userAge = GregorianCalendar(year, month, day)
            val minAdultAge = GregorianCalendar()
            minAdultAge.add(Calendar.YEAR, -18)
            minAdultAge.add(Calendar.MONTH, -1)
            if (minAdultAge.before(userAge)) {
                Toast.makeText(this, getString(R.string.txt_18_years_age_validation), Toast.LENGTH_SHORT).show()
            } else {
                etDob!!.setText(dob)
            }
        }, year, month, day
    )

    dpd.datePicker.maxDate = Calendar.getInstance().timeInMillis
    dpd.show()

What modifications needed to get validations for todays date.

Thank You.

Try turning minAdultAge and the DOB into millis for comparing

minAdultAge.timeInMillis > dob.timeInMillis

I've edited to show how you could validate the exact age based on the day. It's meant as a help not a solution.

public static void main(String[] args) {

    Calendar present = Calendar.getInstance();
    Calendar personBirthDate = Calendar.getInstance();
    personBirthDate.set(Calendar.YEAR, 2001);
    personBirthDate.set(Calendar.DAY_OF_YEAR, personBirthDate.get(Calendar.DAY_OF_YEAR) - 1); // yesterday
    int yearDiff = present.get(Calendar.YEAR) - personBirthDate.get(Calendar.YEAR);
    int dayDiff =  present.get(Calendar.DAY_OF_YEAR) - personBirthDate.get(Calendar.DAY_OF_YEAR);

    System.out.println("Day of person birth year " + personBirthDate.get(Calendar.DAY_OF_YEAR));
    System.out.println("Day of current year " + present.get(Calendar.DAY_OF_YEAR));
    System.out.println("Years between " + yearDiff);

    if(present.get(Calendar.DAY_OF_YEAR) - personBirthDate.get(Calendar.DAY_OF_YEAR) > 0){
        System.out.println("You are only " + (yearDiff - 1) + " years old");
    }else if(present.get(Calendar.DAY_OF_YEAR) - personBirthDate.get(Calendar.DAY_OF_YEAR) < 0) {
        System.out.println("You are already " + yearDiff + " years old");
    }else{
        System.out.println("You are exactly " + yearDiff + " years old");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM