乔达泰姆(Jodatime)是否可以获取自某个较旧日期以来的月份和日期? [英] Does Jodatime have a method to get the months and days since some older date?
问题描述
我想确切地知道某个较旧的日期是从今天算起的几个月和几天(可能是几年).有办法吗?
I would like to know exactly how many months and days(possibly years) some older date is from today. Is there a method to do that?
我知道如何获得月份的差额,我知道如何在几天中获得差额.但是我无法获得月份和日期.
I know how to get the difference of the months, I know how to get the difference in days. But I am unable to get the months and the days.
例如:
old ='2013-03-04' 现在='2013-04-17'
old = '2013-03-04' now = '2013-04-17'
所以我寻找的结果大约是1个月和13 *天
so the result im looking for is something like 1 month(s) and 13* day(s)
*也许不是每个人都能确定它的12个即时消息.
*maybe its 12 im not every sure.
推荐答案
This can be done by using Period in JodaTime.
例如,
LocalDate old = new LocalDate(2013, 3, 4);
LocalDate now = new LocalDate(2013, 4, 17);
Period p = new Period(old, now, PeriodType.yearMonthDay());
要获取月份,请使用p.getMonths()
来获取日期p.getDays()
.
To get the months, use p.getMonths()
, to get the days p.getDays()
.
该示例的结果是1个月13天.
The result of the example is 1 month, 13 days.
这篇关于乔达泰姆(Jodatime)是否可以获取自某个较旧日期以来的月份和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!