Iterate through a range of Dates in Java (#4959)
* Iterating over collection of dates * Development of range dates iteration * Removed some tests * After editor review * Unused interface removed * Second code revision
This commit is contained in:
parent
4e968e1b15
commit
f77e37ef3f
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
public class DatesCollectionIteration {
|
||||
|
||||
public void iteratingRangeOfDatesJava7(Collection<Date> dates) {
|
||||
|
||||
for (Date date : dates) {
|
||||
processDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public void iteratingRangeOfDatesJava8(Collection<Date> dates) {
|
||||
dates.stream()
|
||||
.forEach(this::processDate);
|
||||
}
|
||||
|
||||
private void processDate(Date date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class RangeDatesIteration {
|
||||
|
||||
public void iterateBetweenDatesJava9(LocalDate startDate, LocalDate endDate) {
|
||||
|
||||
startDate.datesUntil(endDate)
|
||||
.forEach(this::processDate);
|
||||
}
|
||||
|
||||
public void iterateBetweenDatesJava8(LocalDate start, LocalDate end) {
|
||||
for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
|
||||
processDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateBetweenDatesJava7(Date start, Date end) {
|
||||
Date current = start;
|
||||
|
||||
while (current.before(end)) {
|
||||
processDate(current);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(current);
|
||||
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
|
||||
current = calendar.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
private void processDate(LocalDate date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
|
||||
private void processDate(Date date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DatesCollectionIterationUnitTest {
|
||||
|
||||
private Collection<LocalDate> localDates = LocalDate.now()
|
||||
.datesUntil(LocalDate.now()
|
||||
.plus(10L, ChronoUnit.DAYS))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
private Collection<Date> dates = localDates.stream()
|
||||
.map(localDate -> Date.from(localDate.atStartOfDay(ZoneId.systemDefault())
|
||||
.toInstant()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
@Test
|
||||
public void givenIteratingListOfDatesJava7_WhenStartTodayAndEnding10DaysAhead() {
|
||||
DatesCollectionIteration iterateInColleciton = new DatesCollectionIteration();
|
||||
Calendar today = Calendar.getInstance();
|
||||
Calendar next10Ahead = (Calendar) today.clone();
|
||||
next10Ahead.add(Calendar.DATE, 10);
|
||||
|
||||
iterateInColleciton.iteratingRangeOfDatesJava7(createRangeDates(today.getTime(), next10Ahead.getTime()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIteratingListOfDatesJava8_WhenStartTodayAndEnd10DaysAhead() {
|
||||
DatesCollectionIteration iterateInColleciton = new DatesCollectionIteration();
|
||||
|
||||
iterateInColleciton.iteratingRangeOfDatesJava8(dates);
|
||||
}
|
||||
|
||||
private List<Date> createRangeDates(Date start, Date end) {
|
||||
|
||||
List<Date> dates = new ArrayList<>();
|
||||
Date current = start;
|
||||
|
||||
while (current.before(end)) {
|
||||
dates.add(current);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(current);
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
|
||||
current = calendar.getTime();
|
||||
}
|
||||
|
||||
return dates;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RangeDatesIterationUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIterateBetweenDatesJava9_WhenStartDateAsTodayAndEndDateAs10DaysAhead() {
|
||||
LocalDate start = LocalDate.now();
|
||||
LocalDate end = start.plus(10L, ChronoUnit.DAYS);
|
||||
|
||||
RangeDatesIteration iteration = new RangeDatesIteration();
|
||||
|
||||
iteration.iterateBetweenDatesJava9(start, end);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIterateBetweenDatesJava8_WhenStartDateAsTodayAndEndDateAs10DaysAhead() {
|
||||
LocalDate start = LocalDate.now();
|
||||
LocalDate end = start.plus(10L, ChronoUnit.DAYS);
|
||||
|
||||
RangeDatesIteration iteration = new RangeDatesIteration();
|
||||
|
||||
iteration.iterateBetweenDatesJava8(start, end);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIterateBetweenDatesJava7_WhenStartDateAsTodayAndEndDateAs10DaysAhead() {
|
||||
Calendar today = Calendar.getInstance();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.clear();
|
||||
calendar.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE));
|
||||
Date start = calendar.getTime();
|
||||
|
||||
calendar.add(Calendar.DATE, 10);
|
||||
Date end = calendar.getTime();
|
||||
|
||||
RangeDatesIteration iteration = new RangeDatesIteration();
|
||||
|
||||
iteration.iterateBetweenDatesJava7(start, end);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue