Answer the question
In order to leave comments, you need to log in
Why is it throwing a runtime-error?
Hello. I can't figure out why it's giving a runtime-error? The code itself works and knows how to break dates into intervals. Attached is a link to the entire code.
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
https://docs.oracle.com/javase/7/docs/api/java/uti...
Throws:
NoSuchElementException - if no more tokens are available
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question