我正在尝试获取两个日期之间剩余的年、月和日: 所以我使用 Joda Time 来这样做:
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
有什么方法可以分别获取这些值而不是获取一个字符串?太感谢了! :)
最佳答案
我曾经有过同样的需求,这里是代码 fragment
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);
关于java - 剩余年、月、日使用joda时间(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38749664/