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
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
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
}
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
}
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
}
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.
Convert the data to a string in dd/mm/yyyy format
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");
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);
numyears = Math.floor(diffInSec/ 31536000)
if(numyears>18){
//your code here
}
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;
}