0

In my app I have 3 edittexts where user enters day (f.e. 23), month (f.e. 09) and year (f.e. 2008). I would like to know how to determine is user younger or older than 18 years.

How to do this?

PS. Please write your solution in Kotlin

Kratos
  • 681
  • 1
  • 13
  • 30
  • Here, [this](https://stackoverflow.com/a/40816583/8244632) is what you need. [The other answer](https://stackoverflow.com/a/44236118/8244632) just below it is also useful. These answers will give you the days difference, now compare it with 18*365 and you'll have your answer. – Lalit Fauzdar Apr 29 '21 at 08:54

5 Answers5

3

Try this code. I have not tested it but It will give you the desired output. Let me know If not

private int getYears() {
int year = 2008; // get from the year edittext
int month = 9; // get from the month edittext
int day = 23; // get from the date edittext
Calendar c1 = Calendar.getInstance();
c1.set(year, month - 1, day, 0, 0); // as MONTH in calender is 0 based.

Calendar c2 = Calendar.getInstance();
int diff = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
if (c1.get(Calendar.MONTH) > c2.get(Calendar.MONTH) ||
        (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) && c1.get(Calendar.DATE) > c2.get(Calendar.DATE))) {
  diff--;
}
return diff;

}

And here is an equivalent Kotlin funtion

private fun getYears(): Int {
    val year = 2008 // get from the year edittext
    val month = 9 // get from the month edittext
    val day = 23 // get from the date edittext
    val c1 = Calendar.getInstance()
    c1[year, month - 1, day, 0] = 0 // as MONTH in calender is 0 based.
    val c2 = Calendar.getInstance()
    var diff = c2[Calendar.YEAR] - c1[Calendar.YEAR]
    if (c1[Calendar.MONTH] > c2[Calendar.MONTH] ||
        c1[Calendar.MONTH] == c2[Calendar.MONTH] && c1[Calendar.DATE] > c2[Calendar.DATE]
    ) {
        diff--
    }
    return diff
}
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
1

This is the correct solutions to your problem, I hope this will help you

and please import these lines also

import java.time.LocalDate
import java.time.Period
import java.util.Locale

fun isUser18Older(): Boolean {
    val todaysDate = LocalDate.now() 
    val userDob = LocalDate.of(2004, 2, 3) //3-Feb-2004
    val yearDiff = Period.between(userDob,todaysDate ).years
    return yearDiff >= 18
}
Nitin Prakash
  • 927
  • 9
  • 16
  • NIce answer but this only works for API level bigger or same as OREO. But I will upvote because it looks simple and nice – Kratos Apr 29 '21 at 10:37
1

In age control, leap years can confuse our calculations. For this reason, I prepared an algorithm in the simplest way. You can use this extension method without any problems with year, month, day comparisons.

fun Date.isUser18Older(): Boolean {

    val toDate = Calendar.getInstance()
    val birthDate = Calendar.getInstance()
    birthDate.time = this

    if (toDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR) > 18) {
        return true
    }

    if (toDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR) == 18) {

        if (toDate.get(Calendar.MONTH) > birthDate.get(Calendar.MONTH)) {
            return true
        }

        if (toDate.get(Calendar.MONTH) == birthDate.get(Calendar.MONTH) && birthDate.get(Calendar.DAY_OF_MONTH) <= toDate.get(
                Calendar.DAY_OF_MONTH
            )
        ) {
            return true
        }

    }
    return false
}
ulaserdegor
  • 371
  • 5
  • 10
0

Ref: 1.https://stackoverflow.com/questions/25460965/how-can-i-create-a-date-object-with-a-specific-format 2.https://stackoverflow.com/questions/10690370/how-do-i-get-difference-between-two-dates-in-android-tried-every-thing-and-pos/63207518

I would suggest doing this in a step-by-step approach.

  1. Convert the data to a string in dd/mm/yyyy format

  2. Now create a date object with that in this fashion

 SimpleDateFormat sdf =  new SimpleDateFormat("dd/mm/yyyy");

    Date d= new Date(); //Get system date

    //Convert Date object to a string
    String strDate = sdf.format(d);

    //Convert a String to Date
    d  = sdf.parse("02/04/2014");
  1. Find the difference between the dates using the following format
long diffInMillisec = date1.getTime() - date2.getTime();

    long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMillisec);
    long diffInHours = TimeUnit.MILLISECONDS.toHours(diffInMillisec);
    long diffInMin = TimeUnit.MILLISECONDS.toMinutes(diffInMillisec);
    long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMillisec);
  1. Convert to years and then check
numyears = Math.floor(diffInSec/ 31536000)
if(numyears>18){
//your code here
}
Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
0

This is the kotlin implementation of the answerr given by @Bhargav

private fun getUserYears() : Int {
        var year = 2003; // get from the year edittext
        var month = 4; // get from the month edittext
        var day = 30; // get from the date edittext
        var c1 = Calendar.getInstance ();
        c1.set(year, month - 1, day, 0, 0); // as MONTH in calender is 0 based.

        var c2 = Calendar.getInstance();
        var diff = c2.get (Calendar.YEAR) - c1.get(Calendar.YEAR);
        if (c1.get(Calendar.MONTH) > c2.get(Calendar.MONTH) ||
            (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) && c1.get(Calendar.DATE) > c2.get(
                Calendar.DATE
            ))
        ) {
            diff--;
        }

        return diff;
    }
Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
Kratos
  • 681
  • 1
  • 13
  • 30