我正在使用period.between函数获取从今天到2020年7月4日之间的日期,但是由于某种原因其仅打印了16天,有人可以告诉我如何使用此函数吗?
这是我的代码:
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
public class Period4th {
public static void main(String[] args) {
Date today = new Date();
Date july4 = new Date();
july4.setDate(4);
july4.setMonth(6);
july4.setYear(2020);
Period daysBetween = Period.between(today.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
july4.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
System.out.println(daysBetween.getDays());
}
}
答案 0 :(得分:3)
package com.example.demo;
import java.time.*;
public class Period4th {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate july4 = LocalDate.of(2020, 6, 4);
System.out.println(Duration.between(today.atStartOfDay(), july4.atStartOfDay()).toDays());
}
}
感谢大家的评论建议,使用java.time包:D