【问题标题】:Remaining years, months, days using joda time (Android)使用 joda 时间的剩余年、月、日 (Android)
【发布时间】:2016-08-03 17:00:41
【问题描述】:

我正在尝试获取两个日期之间剩余的年、月和日: 所以我使用了 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

有没有办法分别获取这些值而不是获取一个字符串?非常感谢! :)

【问题讨论】:

标签: java android


【解决方案1】:

我曾经有同样的要求,这里是代码sn-p

    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);

【讨论】:

    【解决方案2】:

    调用 DateTime 类提供的方法并减去它们。多年来的一个例子如下:

    int year = (int) dateTime#year#getField() - (int) dateTime2#year#getField()

    未经测试的代码!!稍后我会研究它,但总体思路是一样的,获取字段信息然后减去它得到一个值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多