From b9fe921b74f321ec1ec3fd1421670cc2443b4eff Mon Sep 17 00:00:00 2001 From: Dimitris Vasdekis <31992152+BasdekD@users.noreply.github.com> Date: Thu, 14 Mar 2024 23:05:50 +0200 Subject: [PATCH] Fix the calculation of a leap year. Also, fix condition to not consider 28 Feb as invalid for non-leap years --- dateedittext/build.gradle | 2 +- .../main/java/com/msa/dateedittext/DateEditText.kt | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/dateedittext/build.gradle b/dateedittext/build.gradle index 5eb194c..5d63b2f 100644 --- a/dateedittext/build.gradle +++ b/dateedittext/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.library' -apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 29 diff --git a/dateedittext/src/main/java/com/msa/dateedittext/DateEditText.kt b/dateedittext/src/main/java/com/msa/dateedittext/DateEditText.kt index dbf50d5..d46052d 100644 --- a/dateedittext/src/main/java/com/msa/dateedittext/DateEditText.kt +++ b/dateedittext/src/main/java/com/msa/dateedittext/DateEditText.kt @@ -197,7 +197,7 @@ class DateEditText : TextInputEditText { val day = date.substring(0, 2).toInt() val month = date.substring(3, 5).toInt() val year = date.substring(6, 10).toInt() - val isLeapYear = (year % 100 != 0 || year % 400 != 0) + val isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) if (month > 12 || month <= 0) { throw IllegalArgumentException("Invalid date") @@ -300,6 +300,7 @@ class DateEditText : TextInputEditText { setError(value= mValue.substring(0, 2), errorMessage = context.getString(R.string.invalid_day)) } } + } // validate month @@ -320,7 +321,7 @@ class DateEditText : TextInputEditText { }else{ setError(value= mValue.substring(0,5), errorMessage = context.getString(R.string.invalid_day_of_month)) } - } else if (month == 2 && day == 31) { + } else if (month == 2 && day > 29) { if (autoCorrect){ mValue = mValue.replace(day.toString(), "29", false) }else{ @@ -360,11 +361,11 @@ class DateEditText : TextInputEditText { if (isLeapYear(year).not()) { val month = mValue.substring(3, 5).toInt() val day = mValue.substring(0, 2).toInt() - if (month == 2 && day >= 28) { + if (month == 2 && day > 28) { if (autoCorrect){ mValue = mValue.replace(day.toString(), "28", false) - }else{ - setError(value= mValue, errorMessage = context.getString(R.string.invalid_day_of_month_leap_year)) + } else { + setError(value= mValue, errorMessage = context.getString(R.string.invalid_day_of_month)) } } }