java-如何从某个日期获取日期列表?
作者:互联网
这个问题已经在这里有了答案: > how to get a list of dates between two dates in java 21个
我有两个变量:
> startDate-例如29/04/2018
> howManyDays-例如30
我想从日期29/04/2018得到30天的清单.你能告诉我怎么做吗?我发现beetwen只有两个日期.
int days = Days.daysBetween(startDate, endDate).getDays();
List<LocalDate> dates = new ArrayList<LocalDate>(days); // Set initial capacity to `days`.
for (int i=0; i < days; i++) {
LocalDate d = startDate.withFieldAdded(DurationFieldType.days(), i);
dates.add(d);
}
我不知道如何更改代码.
解决方法:
您可以使用此示例代码获取从特定日期开始的30个日期.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class Days {
public static void main(String[] args) {
String startDate = "29/04/2018";
int howManyDays = 30;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse(startDate, formatter);
List<LocalDate> dates = new ArrayList<>();
for (int i = 0; i < howManyDays; i++) {
dates.add(localDate.plusDays(i));
}
//For check
dates.forEach(System.out::println);
}
}
运行howManyDays = 5可以得到:
2018-04-29
2018-04-30
2018-05-01
2018-05-02
2018-05-03
标签:android,days,java,jodatime,android-jodatime 来源: https://codeday.me/bug/20191013/1910647.html