无法使用 joda time 获得两个日期之间的正确差异
Not able to get correct difference between two dates using joda time
我正在尝试使用 joda time 获取两个日期之间的差异,但不知何故我无法获得确切的差异。
LocalDate endofCentury = new LocalDate(2014, 01, 01);
LocalDate now = LocalDate.now(); //2017-04-11
Period diff = new Period(endofCentury, now);
System.out.printf("Difference is %d years, %d months and %d days old",
diff.getYears(), diff.getMonths(), diff.getDays());
差异应该是 3 年 3 个月 10 天 但我得到 3 年 3 个月零 3 天 .
不确定我缺少什么,请帮助我。
谢谢
使用带 3 个参数的构造函数:
Period diff = new Period(endofCentury, now, PeriodType.yearMonthDay());
带 2 个参数的构造函数 (from,to)
包括周数。
所以你的代码的修改输出:
Period diff = new Period(endofCentury, now);
System.out.printf("Difference is %d years, %d months and %d weeks and %d days old",
diff.getYears(), diff.getMonths(),diff.getWeeks(), diff.getDays());
给出输出:
Difference is 3 years, 3 months and 1 weeks and 3 days old
但具有指定的持续时间字段(第 3 个参数):
Period diff = new Period(endofCentury, now, PeriodType.yearMonthDay());
System.out.printf("Difference is %d years, %d months and %d weeks and %d days old",
diff.getYears(), diff.getMonths(),diff.getWeeks(), diff.getDays());
你得到:
Difference is 3 years, 3 months and 0 weeks and 10 days old
参见:http://joda-time.sourceforge.net/apidocs/org/joda/time/PeriodType.html
我正在尝试使用 joda time 获取两个日期之间的差异,但不知何故我无法获得确切的差异。
LocalDate endofCentury = new LocalDate(2014, 01, 01);
LocalDate now = LocalDate.now(); //2017-04-11
Period diff = new Period(endofCentury, now);
System.out.printf("Difference is %d years, %d months and %d days old",
diff.getYears(), diff.getMonths(), diff.getDays());
差异应该是 3 年 3 个月 10 天 但我得到 3 年 3 个月零 3 天 .
不确定我缺少什么,请帮助我。
谢谢
使用带 3 个参数的构造函数:
Period diff = new Period(endofCentury, now, PeriodType.yearMonthDay());
带 2 个参数的构造函数 (from,to)
包括周数。
所以你的代码的修改输出:
Period diff = new Period(endofCentury, now);
System.out.printf("Difference is %d years, %d months and %d weeks and %d days old",
diff.getYears(), diff.getMonths(),diff.getWeeks(), diff.getDays());
给出输出:
Difference is 3 years, 3 months and 1 weeks and 3 days old
但具有指定的持续时间字段(第 3 个参数):
Period diff = new Period(endofCentury, now, PeriodType.yearMonthDay());
System.out.printf("Difference is %d years, %d months and %d weeks and %d days old",
diff.getYears(), diff.getMonths(),diff.getWeeks(), diff.getDays());
你得到:
Difference is 3 years, 3 months and 0 weeks and 10 days old
参见:http://joda-time.sourceforge.net/apidocs/org/joda/time/PeriodType.html