# Joda-Time，Period计算问题？

go评论49阅读模式

Joda-time, Period calculate issue?

# 问题

``````val date1 = org.joda.time.LocalDate(2025, 2, 28)
val date2 = org.joda.time.LocalDate(2019, 11, 28).plusMonths(3)
val ddd = org.joda.time.Period(date2, date1)
println("\${ddd.years} \${ddd.months} \${ddd.days}")

val date1 = org.joda.time.LocalDate(2025, 2, 28)
val date2 = org.joda.time.LocalDate(2019, 11, 29).plusMonths(3)
val ddd = org.joda.time.Period(date2, date1)
println("\${ddd.years} \${ddd.months} \${ddd.days}")

val date1 = org.joda.time.LocalDate(2025, 2, 28)
val date2 = org.joda.time.LocalDate(2019, 11, 30).plusMonths(3)
val ddd = org.joda.time.Period(date2, date1)
println("\${ddd.years} \${ddd.months} \${ddd.days}")
``````

the below codes returns same result, a 5 years difference.
should be 5Y, 5Y1D and 5Y2D?

``````val date1 = org.joda.time.LocalDate(2025,2,28)
val date2 = org.joda.time.LocalDate(2019,11,28).plusMonths(3)
val ddd = org.joda.time.Period(date2, date1)
println(&quot;\${ddd.years} \${ddd.months} \${ddd.days}&quot;)

val date1 = org.joda.time.LocalDate(2025,2,28)
val date2 = org.joda.time.LocalDate(2019,11,29).plusMonths(3)
val ddd = org.joda.time.Period(date2, date1)
println(&quot;\${ddd.years} \${ddd.months} \${ddd.days}&quot;)

val date1 = org.joda.time.LocalDate(2025,2,28)
val date2 = org.joda.time.LocalDate(2019,11,30).plusMonths(3)
val ddd = org.joda.time.Period(date2, date1)
println(&quot;\${ddd.years} \${ddd.months} \${ddd.days}&quot;)
``````

# 答案1

No, it should not.

First, when adding three months to either 28, 29 or 30 November 2019, you are getting 28, 29 and 29(!) February 2020. 2020 is a leap year, so February has 29 days, not 30. Next, what is the period from 29 February 2020 to 28 February 2025? It’s not really well defined. You might argue that it is less than 5 years, perhaps 4 years 11 months 30 days? However, Joda-Time has chosen to regard it as 5 years since both dates are at the end of the month.

# 答案2

``````fun main() {
println(
"Between \${javaDate2} and \${javaDate1} are \${jd.years} years, \${jd.months} months and \${jd.days} days")

println(
"Between \${javaDate2} and \${javaDate1} are \${jd.years} years, \${jd.months} months and \${jd.days} days")

println(
"Between \${javaDate2} and \${javaDate1} are \${jd.years} years, \${jd.months} months and \${jd.days} days")
}
``````

``````Between 2020-02-28 and 2025-02-28 are 5 years, 0 months and 0 days
Between 2020-02-29 and 2025-02-28 are 4 years, 11 months and 30 days
Between 2020-02-29 and 2025-02-28 are 4 years, 11 months and 30 days
``````

I recommend the use of `java.time` for more accurate results.

You can refactor your code to this:

``````fun main() {
println(
&quot;Between \${javaDate2} and \${javaDate1} are \${jd.years} years, \${jd.months} months and \${jd.days} days&quot;)

println(
&quot;Between \${javaDate2} and \${javaDate1} are \${jd.years} years, \${jd.months} months and \${jd.days} days&quot;)

println(
&quot;Between \${javaDate2} and \${javaDate1} are \${jd.years} years, \${jd.months} months and \${jd.days} days&quot;)
}
``````

run it and get the following results:

``````Between 2020-02-28 and 2025-02-28 are 5 years, 0 months and 0 days
Between 2020-02-29 and 2025-02-28 are 4 years, 11 months and 30 days
Between 2020-02-29 and 2025-02-28 are 4 years, 11 months and 30 days
``````

• 本文由 发表于 2020年5月5日 14:17:40
• 转载请务必保留本文链接：https://go.coder-hub.com/61606890.html
• java
• jodatime
• kotlin

go 57

go 46

go 46

go 55