Hun*_*gnn 1 java date java-time
我正在研究 Java Date(Java 8)。
我有 2 个日期,例如:2023-02-22和2023-02-28
我的预期输出包含:
2023-02-22,2023-02-23,2023-02-24,2023-02-25,2023-02-26,2023-02-27,2023-02-28
日期格式:yyyy-MM-dd
我的代码:
public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
List<String> dates = new ArrayList<>();
// get dates between 2 dates
return dates;
}
Run Code Online (Sandbox Code Playgroud)
如何从该日期获取列表日期?谢谢,
您可以使用LocalDate::datesUntil从Java9引入的:
\n\n\n
public Stream<LocalDate> datesUntil\xe2\x80\x8b(LocalDate endExclusive)
\n返回顺序排列的日期流。返回的流从该日期(含)开始,到 endExclusive(不含)\n以 1 天的增量步长结束。
该方法相当于\ndatesUntil(endExclusive, Period.ofDays(1))。
public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate){\n LocalDate startDate = LocalDate.parse(fromDate);\n LocalDate endDate = LocalDate.parse(toDate);\n return startDate.datesUntil(endDate.plusDays(1))\n .map(LocalDate::toString)\n .collect(Collectors.toList());\n}\nRun Code Online (Sandbox Code Playgroud)\n我注意到您编辑问题以寻求使用 Java8 的解决方案,在这种情况下,您可以使用:
\nLocalDate startDate = LocalDate.parse(fromDate);\nLocalDate endDate = LocalDate.parse(toDate);\nreturn Stream.iterate(startDate, d -> d.plusDays(1))\n .limit(ChronoUnit.DAYS.between(startDate, endDate.plusDays(1)))\n .map(LocalDate::toString)\n .collect(Collectors.toList());\nRun Code Online (Sandbox Code Playgroud)\n
首先,用于LocalDate处理日期。每当你处理日期/时间时,你应该强烈考虑使用java.timeover Strings。您可以使用以下方法将Strings 转换为s :LocalDateLocalDate.parse
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(fromDate, formatter);
LocalDate endDate = LocalDate.parse(toDate, formatter);
Run Code Online (Sandbox Code Playgroud)
然后,您可以一遍又一遍地添加一天,直到达到相同的日期:
for(LocalDate date = startDate; !endDate.isBefore(date); date = date.plusDays(1)){
dates.add(date.format(formatter));
//dates.add(date);//if you want to work with a List<LocalDate>
}
Run Code Online (Sandbox Code Playgroud)
总而言之,您可以在您的方法中使用它,如下所示:
public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
List<String> dates = new ArrayList<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(fromDate, formatter);
LocalDate endDate = LocalDate.parse(toDate, formatter);
for(LocalDate date = startDate; !endDate.isBefore(date); date = date.plusDays(1)){
dates.add(date.format(formatter));
}
return dates;
}
Run Code Online (Sandbox Code Playgroud)
正如评论中提到的 shmosel一样,默认情况下LocalDate使用 a ISO_LOCAL_DATE,因此您不必DateTimeFormatter在您的情况下提供 a :
public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
List<String> dates = new ArrayList<>();
LocalDate startDate = LocalDate.parse(fromDate);
LocalDate endDate = LocalDate.parse(toDate);
for(LocalDate date = startDate; !endDate.isBefore(date); date = date.plusDays(1)){
dates.add(date.toString());
}
return dates;
}
Run Code Online (Sandbox Code Playgroud)