Java以十进制年数表示的时间段 [英] Java Time period in decimal number of years

查看:116
本文介绍了Java以十进制年数表示的时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下方法计算 java.time 中2 LocalDate 之间的差异:

If I calculate the difference between 2 LocalDate's in java.time using:

Period p = Period.between(testDate, today);

然后我得到一个年,月,天数的输出:

Then I get an output with the number of years, months, days like:

Days = 9
Months = 6
Years = 18

有没有人知道一种干净的方式来表示它作为十进制类型值(即,上面将是 18.5 ...

Does anyone know a clean way to represent that as a decimal type value (ie, above would be something around 18.5...)?

推荐答案

我会避免使用Period,而只是计算天数的差异:

I would avoid using Period, and instead just calculate the difference in days:

float years = testDate1.until(today, ChronoUnit.DAYS) / 365.2425f;

这篇关于Java以十进制年数表示的时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆