时间相减 示例kotlin LocalDateTime
fun test1(){
val d1: LocalDateTime = LocalDateTime.parse("2023-05-06 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val d2: LocalDateTime = LocalDateTime.parse("2023-05-07 15:12:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val d3: LocalDate = LocalDate.parse("2023-05-06 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val d4: LocalDate = LocalDate.parse("2023-05-07 15:12:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val diff: Duration = Duration.between(d1, d2)
val period: Period = Period.between(d3, d4)
val diffDays: Long = diff.toDays()
val years: Int = Math.abs(period.getYears())
val months: Int = Math.abs(period.getMonths())
val days: Int = Math.abs(period.getDays())
println("Diffrence between dates is : " + diffDays + "days")
println("Diffrence is : $years year, $months months, $days days")
println(d1.month.value)
}
结果
Diffrence between dates is : 1days
Diffrence is : 0 year, 0 months, 1 days
5