String timeStart = "2016-12-11";
String timeEnd = "2016-12-20";
已知两个日期,怎么循环出 [timeStart - timeEnd] 的所有日期,包括开始和结束时间。
并存入一个List<String>的集合中
PHPz2017-04-18 10:32:22
Haha, it’s a date problem again, then I definitely recommend you to use the new time API of Java 8, and your time string is still in this formatyyyy-MM-dd
,直接LocalDate.parse
The method can convert the string into a LocalDate object
Furthermore, if this involves a series of regular time collections, you must consider Stream. It is very convenient and fast to use Stream to construct your collection. The following is the sample code:
/**
* 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
* @param timeStart
* @param timeEnd
* @return
*/
public static List<String> collectLocalDates(String timeStart, String timeEnd){
return collectLocalDates(LocalDate.parse(timeStart), LocalDate.parse(timeEnd));
}
/**
* 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
* @param start
* @param end
* @return
*/
public static List<String> collectLocalDates(LocalDate start, LocalDate end){
// 用起始时间作为流的源头,按照每次加一天的方式创建一个无限流
return Stream.iterate(start, localDate -> localDate.plusDays(1))
// 截断无限流,长度为起始时间和结束时间的差+1个
.limit(ChronoUnit.DAYS.between(start, end) + 1)
// 由于最后要的是字符串,所以map转换一下
.map(LocalDate::toString)
// 把流收集为List
.collect(Collectors.toList());
}
Then the test code:
String timeStart = "2016-12-11";
String timeEnd = "2016-12-20";
collectLocalDates(timeStart, timeEnd).forEach(System.out::println);
The following is the print result:
Easy to use~~Perfectly elegant and easy to understand Java8~Haha
阿神2017-04-18 10:32:22
You calculate the number of days difference between two dates and add 1 in the loop. If the added date is equal to the later date, it will be fine
PHP中文网2017-04-18 10:32:22
思路就是这样:
public static List<Integer> getYear(int startYear,int endYear) {
List<Integer> years = new ArrayList<Integer>();
while (startYear <= endYear) {
years.add(startYear);
startYear++;
}
return years;
}
PHP中文网2017-04-18 10:32:22
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date begin = sdf.parse("2016-12-11");
Date end = sdf.parse("2016-12-20");
List<String> l = new ArrayList<String>();
Calendar cal = Calendar.getInstance();
while (begin.before(end)) {
l.add(sdf.format(begin));
cal.setTime(begin);
cal.add(Calendar.DAY_OF_MONTH, 1);
begin = cal.getTime();
}
for(String s:l){
System.out.println(s);
}
} catch (ParseException e) {
e.printStackTrace();
}
伊谢尔伦2017-04-18 10:32:22
You need Apache’s lang package, which has the following API
static Date addDays(Date date, int amount) Returns a new Date object after adding amount days to a date time object
static Date addHours(Date date, int amount) Returns a new Date object after adding amount h to a date time object
static Date addMilliseconds(Date date, int amount) Returns a new Date object after adding amount milliseconds to a date time object
static Date addMinutes(Date date, int amount) Returns a new Date object after adding amount minutes to a date time object
static Date addMonths(Date date, int amount) Returns a new Date object after adding amount months to a date time object
static Date addSeconds(Date date, int amount) Returns a new Date object after adding amount seconds to a date time object
static Date addWeeks(Date date, int amount) Returns a new Date object after adding amount weeks to a date time object
static Date addYears(Date date, int amount) Returns a new Date object after adding amount years to a date time object
Article address