如何在Java中计算两个日期之间的天数
让我们首先设置两个日期:
LocalDate date1 = LocalDate.of(2019, 4, 16);LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);
现在,使用来计算上述两个日期之间的日期between()
:
int numDays = Period.between(date1, date2).getDays();
示例
import java.time.LocalDate;import java.time.Month;
import java.time.Period;
public class Demo {
public static void main(String[] argv) {
LocalDate date1 = LocalDate.of(2019, 4, 16);
LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);
int numDays = Period.between(date1, date2).getDays();
System.out.println("Number of days between two dates = "+numDays);
}
}
输出结果
Number of days between two dates = 18
以上是 如何在Java中计算两个日期之间的天数 的全部内容, 来源链接: utcz.com/z/350194.html