开始日期:“2021-08-31”
结束日期:“2021-11-30”
在上述两个日期之间的91天持续时间,期望代码返回3个月的持续时间,但是以下方法仅返回2个月。这是Java 8中的bug 吗?日期为91天,却仅返回2个月。
Period diff = Period.between(LocalDate.parse("2021-08-31"),
LocalDate.parse("2021-11-30"));
long daysBetween = ChronoUnit.MONTHS.between(LocalDate.parse("2021-08-31"),
LocalDate.parse("2021-11-30"));
我尝试使用Joda库而不是Java 8 API,它可以工作。它会返回3,看起来Java持续时间的月份计算也使用了天数。
LocalDate dateBefore= LocalDate.parse("2016-08-31");
LocalDate dateAfter = LocalDate.parse("2016-11-30");
int months = Months.monthsBetween(dateBefore, dateAfter).getMonths();
System.out.println(months);
但我就想用 JDK 的 API。因此仍在寻找其他解决方案。
由于仅需要两个日期之间的月份数,请使用期间的文档来调整日期,如Jacob所述使用日期。只需将两个实例的日期设置为相同的值(每月的第一天)
Period diff = Period.between(
LocalDate.parse("2021-08-31").withDayOfMonth(1),
LocalDate.parse("2021-11-30").withDayOfMonth(1));
System.out.println(diff); //P3M
与其他解决方案相同:
long monthsBetween = ChronoUnit.MONTHS.between(
LocalDate.parse("2021-08-31").withDayOfMonth(1),
LocalDate.parse("2021-11-30").withDayOfMonth(1));
System.out.println(monthsBetween); //3
Period#between的文档指出以下内容:
The start date is included, but the end date is not.
此外:
A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
你的月底结束日期30不大于或等于您的月底开始日期31,因此不考虑第三个月。
注意参数名称:
public static Period between?(LocalDate startDateInclusive, LocalDate endDateExclusive)
要返回3个月,您可以将endDateExclusive增加一天。
因此,最好调整实施方式,使结束日期包括在内并获得所需的结果
Period diff = Period.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30").plusDays(1));
System.out.println("Months :" + diff.getMonths());
//Output -> Months : 3
向后兼容较旧的JDK 版本
public static int monthsBetween(Date d1, Date d2){
if(d2==null || d1==null){
return -1;//Error
}
Calendar m_calendar=Calendar.getInstance()
m_calendar.setTime(d1);
int nMonth1=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
m_calendar.setTime(d2);
int nMonth2=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
return java.lang.Math.abs(nMonth2-nMonth1);
}
永远不要使用LocalDateTime来计算两个日期之间的月份,结果奇怪且不正确,请始终使用LocalDate!
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class TestMonthsDateTime {
public static void main(String[] args) {
/**------------------Date Time----------------------------*/
LocalDateTime t1 = LocalDateTime.now();
LocalDateTime t2 = LocalDateTime.now().minusMonths(3);
long dateTimeDiff = ChronoUnit.MONTHS.between(t2, t1);
System.out.println("diff dateTime :" + dateTimeDiff); //diff dateTime : 2
/**-------------------------Date----------------------------*/
LocalDate t3 = LocalDate.now();
LocalDate t4 = LocalDate.now().minusMonths(3);
long dateDiff = ChronoUnit.MONTHS.between(t4, t3);
System.out.println("diff date :" + dateDiff);//diff date : 3
}
}
private static long durationMonths(LocalDate dateBefore, LocalDate dateAfter) {
System.out.println(dateBefore+" "+dateAfter);
if (dateBefore.getDayOfMonth() > 28) {
dateBefore = dateBefore.minusDays(5);
} else if (dateAfter.getDayOfMonth() > 28) {
dateAfter = dateAfter.minusDays(5);
}
return ChronoUnit.MONTHS.between(dateBefore, dateAfter);
}