让我们首先设置两个日期:
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