Всем привет. Не могу понять, почему выдаёт ошибку runtime-error? Сам код работает и умеет разбивать даты на интервалы.
Ссылку на весь код прилагаю.
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalField;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String type = scanner.next();
LocalDate start = LocalDate.parse(scanner.next());
LocalDate end = LocalDate.parse(scanner.next());
ArrayList<Interval> intervals = new ArrayList<>();
switch (type) {
case "WEEK": {
LocalDate n = start.with(ChronoField.DAY_OF_WEEK, 7);
while (n.isBefore(end)) {
intervals.add(new Interval(start, n));
start = n.plusDays(1);
n = n.plusWeeks(1);
}
intervals.add(new Interval(start, end));
break;
}
case "MONTH": {
LocalDate n = start.withDayOfMonth(start.lengthOfMonth());
while (n.isBefore(end)) {
intervals.add(new Interval(start, n));
start = n.plusDays(1);
n = n.withDayOfMonth(1).plusMonths(1);
n = n.withDayOfMonth(n.lengthOfMonth());
}
intervals.add(new Interval(start, end));
break;
}
case "YEAR": {
LocalDate n = start.withDayOfYear(start.lengthOfYear());
while (n.isBefore(end)) {
intervals.add(new Interval(start, n));
start = n.plusDays(1);
n = n.withDayOfYear(1).plusYears(1);
n = n.withDayOfYear(n.lengthOfYear());
}
intervals.add(new Interval(start, end));
break;
}
case "QUARTER": {
LocalDate n = start.withMonth((start.getMonthValue() / 3) * 3 + 3);
n = n.withDayOfMonth(n.lengthOfMonth());
while (n.isBefore(end)) {
intervals.add(new Interval(start, n));
start = n.plusDays(1);
n = n.withDayOfMonth(1).plusMonths(3);
n = n.withDayOfMonth(n.lengthOfMonth());
}
intervals.add(new Interval(start, end));
break;
}
case "LAST_SUNDAY_OF_YEAR": {
LocalDate n = start.withDayOfYear(start.lengthOfYear());
while (n.getDayOfWeek() != DayOfWeek.SATURDAY)
n = n.minusDays(1);
while (n.isBefore(end)) {
intervals.add(new Interval(start, n));
start = n.plusDays(1);
n = n.plusYears(1);
n = n.withDayOfYear(n.lengthOfYear());
while (n.getDayOfWeek() != DayOfWeek.SATURDAY)
n = n.minusDays(1);
}
intervals.add(new Interval(start, end));
break;
}
}
System.out.println(intervals.size());
for (Interval i : intervals) {
i.print(System.out);
System.out.println();
}
}
static class Interval {
LocalDate start, end;
Interval(LocalDate s, LocalDate e) {
this.start = s;
this.end = e;
}
void print(PrintStream stream) {
stream.print(this.start + " " + this.end);
}
}
}