不带负数的字符串中的当前句点

Present period in a string without negative numbers

在一个基于 joda-time 的项目中,我试图以可读的格式向用户展示两个日期之间的时间段。 如果没有负值,我无法成功检索到这段时间。以下代码片段显示了该问题:

LocalDateTime firstTime = new LocalDateTime( 2014, 10, 15, 4, 42 );
LocalDateTime secondTime = new LocalDateTime( 2014, 12, 3, 5, 5 );

Period period = Period.fieldDifference(firstTime, secondTime).normalizedStandard();

period.toString()的输出是:

P2M-1W-4DT-23H-37M

我正在寻找以如下格式获取句号的最佳方法:

P1M3W 等而不是负值?

感谢您的帮助。

将 Period.fieldDifference 替换为新的 Period。 试试这个:

public static void main(String[] args) {
    LocalDateTime firstTime = new LocalDateTime(2014, 10, 15, 4, 42);
    LocalDateTime secondTime = new LocalDateTime(2014, 12, 3, 5, 5);

    System.out.println("period Type: "+Period.fieldDifference(firstTime, secondTime).getPeriodType());
    Period period = new Period(firstTime, secondTime).normalizedStandard();
    System.out.println(period);
}