forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
47 lines (31 loc) · 1.19 KB
/
Copy pathMain.java
File metadata and controls
47 lines (31 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package modern.challenge;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
System.out.println("Before JDK 8");
Calendar calendar = Calendar.getInstance();
calendar.set(2019, 1, 1);
Date startDate = calendar.getTime();
calendar.set(2019, 1, 21);
Date endDate = calendar.getTime();
Date day = startDate;
while (day.before(endDate)) {
// do something with this day
System.out.println(day);
calendar.setTime(day);
calendar.add(Calendar.DATE, 1);
day = calendar.getTime();
}
System.out.println("\nStarting with JDK 8");
LocalDate startLocalDate = LocalDate.of(2019, 2, 1);
LocalDate endLocalDate = LocalDate.of(2019, 2, 21);
for (LocalDate date = startLocalDate; date.isBefore(endLocalDate); date = date.plusDays(1)) {
// do something with this day
System.out.println(date);
}
System.out.println("\nStarting with JDK 9");
startLocalDate.datesUntil(endLocalDate).forEach(System.out::println);
}
}