입력한 생년월일을 기준으로 만 나이를 계산하는 코드다. calculateAge(date) >= 14
일경우, 만 14세 이상 조건을 만족하게 된다.
/** * 생년월일을 기준으로 현재 나이 계산 * @param unix unixtimestamp */ fun calculateAge(date: Date?): Int { val birthCalendar = Calendar.getInstance() birthCalendar.time = date ?: Date() val current = Calendar.getInstance() val currentYear = current[Calendar.YEAR] val currentMonth = current[Calendar.MONTH] val currentDay = current[Calendar.DAY_OF_MONTH] var age = currentYear - birthCalendar[Calendar.YEAR] if (birthCalendar[Calendar.MONTH] * 100 + birthCalendar[Calendar.DAY_OF_MONTH] > currentMonth * 100 + currentDay ) age-- return age }
'프로그래밍 > Android' 카테고리의 다른 글
[안드로이드] Firebase Crashlytics 연동방법 (0) | 2021.01.15 |
---|---|
[안드로이드] 프로젝트에 Sentry 연동방법, Proguard 적용까지 (0) | 2021.01.12 |
[안드로이드] 주식 봉차트 그리기 (Drawing candle stick chart with MPAndroidChart Library Example) (0) | 2021.01.10 |
[안드로이드] 주식차트 그리기 (Drawing LineChart with MPAndroidChart, Differentiate line colors by limit line value) (2) | 2021.01.09 |
[Java/Kotlin] 안드로이드 DP to PX 변환하기 (0) | 2021.01.08 |