Java 乔达时间怎么计算还剩多少时间?

Java 乔达时间怎么计算还剩多少时间?,java,jodatime,periodformatter,Java,Jodatime,Periodformatter,在我的代码的这一部分中,我希望Joda有时间计算并显示离下一个生日还有多少时间。因此,生日这一年应该有所改变 DateTime startDate = DateTime.now(); DateTime endDate = new DateTime(x,m110,d110,h120,min120); Period period = new Period(startDate, endDate, PeriodType.dayTime()); textView3.setText(formatter.pr

在我的代码的这一部分中,我希望Joda有时间计算并显示离下一个生日还有多少时间。因此,生日这一年应该有所改变

DateTime startDate = DateTime.now();
DateTime endDate = new DateTime(x,m110,d110,h120,min120);
Period period = new Period(startDate, endDate, PeriodType.dayTime());
textView3.setText(formatter.print(period));
PeriodFormatter formatter = new PeriodFormatterBuilder()
                    .appendMonths().appendSuffix(".")
                    .appendDays().appendSuffix(" ")
                    .appendHours().appendSuffix(":")
                    .appendMinutes().appendSuffix(":")
                    .appendSeconds()
                    .toFormatter();
这里的x是年,m110,d110,h120,min120-月,日,小时和分钟。我应该写什么来代替x,这样它就可以计算出每年剩下多少时间,而不是一次。还有一个问题。例如,如果是3小时4分钟,我应该怎么做才能显示03:04:00而不是3:4:它也不会显示0。x的值取决于今年是否已经过生日。您可以使用DateTime.before方法在当前年份的同一天和同一个月检查此项:

DateTime startDate = DateTime.now();
int year = startDate.getYear();
DateTime endDate = new DateTime(year,m110,d110,h120,min120);
if (endDate.isBefore(startDate)) // birthday happend already this year?
    endDate = endDate.plusYears(1); // then the next one is in the next year
关于你的第二个问题: 您可以建议创建一个始终使用printZeroAlways打印零的格式化程序。要使格式化程序打印至少两位数字,可以使用minimumPrintedDigits方法:

请注意,这些方法仅适用于调用该方法后添加的字段。此外,该值可能会随着生成器的创建而多次更改

   PeriodFormatter formatter = new PeriodFormatterBuilder()            
        .minimumPrintedDigits(0)

        .appendMonths().appendSuffix(".")
        .printZeroAlways()
        .appendDays().appendSuffix(" ")

        .minimumPrintedDigits(2)

        .appendHours().appendSuffix(":")
        .appendMinutes().appendSuffix(":")
        .appendSeconds()
        .toFormatter();