我正在尝试获取两个日期之间的剩余年、月和天:所以我使用Joda时间来实现:
DateTime endDate = new DateTime(2018,12,25,0,0);
DateTime startDate = new DateTime();
Period period = new Period(startDate,endDate,PeriodType.yearMonthDay());
PeriodFormatter formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" Year ").
appendMonths().appendSuffix(" Month ").appendDays().appendSuffix(" Day ").appendHours()..toFormatter();
String time = formatter.print(period);这给了我字符串时间:2年4个月22天
但是,我想要剩余的年数、月数、天数的整数值。所以,我想设置我的变量,而不是“2年4个月22天”:
int year = 2
int month = 4
int day = 22有没有办法单独获取这些值,而不是获取一个字符串?非常感谢!:)
发布于 2016-08-04 01:12:39
我曾经有过同样的需求,下面是代码片段
LocalDate d=LocalDate.of(yy,mm,dd);
LocalDate d2=LocalDate.of(yy, mm, dd);
Period p=Period.between(d, d2);
long day,month,year;
day=p.getDays();
month=p.getMonths();
year=p.getYears();
System.out.println(day+" : "+month+" : "+year);发布于 2016-08-04 01:04:52
调用DateTime类提供的方法并减去它们。下面是一个多年的例子:
int year = (int) dateTime#year#getField() - (int) dateTime2#year#getField()
未经测试的代码!!我将在稍后研究它,但总体思路是相同的,获取字段信息,然后减去它就可以得到一个值
https://stackoverflow.com/questions/38749664
复制相似问题