![](/img/trans.png)
[英]how to calculate the exact age of a person from date of birth in android
[英]How to calculate the exact days in Age Calculator between the date entered & the current date?
val theDate=sdf.parse(selectedDate)
theDate?.let {
val selectedDateInMin=theDate.time / 60000
val selectedDateInYear=(theDate.time/ 31536000000)
val selectedDateInMonth=(theDate.time)/2629746000
val selectedDateInDay=(theDate.time)/86400000
val currentDate=sdf.parse(sdf.format(System.currentTimeMillis()))
currentDate?.let {
val currentDateInMin=currentDate.time/60000
val currentDateInYear=(currentDate.time/ 31536000000)
val currentDateInMonth=(currentDate.time)/2629746000
val currentDateInDay=(currentDate.time)/86400000
val differenceInMin = currentDateInMin-selectedDateInMin
val differenceInYear=currentDateInYear-selectedDateInYear-1
val leap_days=(differenceInYear*(0.24)).toInt()
val differenceInMonth=currentDateInMonth-selectedDateInMonth-(differenceInYear*12)
val differenceInDay=currentDateInDay-selectedDateInDay-(differenceInYear*365)-(differenceInMonth*31)-leap_days
当我计算天数差异时,我必须减去月差中包含的天数。 那么我将如何计算该月是否有 28 天或 31 天或 30 天,什么没有。 我必须乘以差异InMonth? 我将如何概括获取特定月份中的确切天数的公式,例如 2 个月或 4 个月有多少天?
import java.time.Duration
val sdf = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
val theDate= sdf.parse("2022-03-14 17:09:18")
val now = sdf.parse(sdf.format(System.currentTimeMillis()))
val duration = Duration.between(now.toInstant(), theDate.toInstant()).toDays()
println("$duration days")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.