Calendar calendar = Calendar.getInstance();
final DateFormat DATE_FORMAT = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
int referenceDayOfYear = -1;
int referenceYear = -1;
for (int i = 0; i < periodSize; i++) {
Period period = new Period();
period.setId((long) i);
String line = bufferedReader.readLine();
String[] lineTokens = line.split(SPLIT_REGEX);
if (lineTokens.length != 4) {
throw new IllegalArgumentException("Read line (" + line + ") is expected to contain 4 tokens.");
}
String startDateTimeString = lineTokens[0] + " " + lineTokens[1];
period.setStartDateTimeString(startDateTimeString);
period.setPeriodIndex(i);
int dayOfYear;
int year;
try {
calendar.setTime(DATE_FORMAT.parse(startDateTimeString));
calendar.get(Calendar.DAY_OF_YEAR);
dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
year = calendar.get(Calendar.YEAR);
} catch (ParseException e) {
throw new IllegalArgumentException("Illegal startDateTimeString (" + startDateTimeString + ").", e);
}
if (referenceDayOfYear < 0) {
referenceDayOfYear = dayOfYear;
referenceYear = year;
}
if (year != referenceYear) {
// Because the Calendar API in JSE sucks... (java 7 will fix that FINALLY)
throw new IllegalStateException("Not yet implemented to handle periods spread over different years...");
}
int dayIndex = dayOfYear - referenceDayOfYear;
if (dayIndex < 0) {
throw new IllegalStateException("The periods should be in ascending order.");
}
period.setDayIndex(dayIndex);
period.setDuration(Integer.parseInt(lineTokens[2]));
period.setPenalty(Integer.parseInt(lineTokens[3]));
periodList.add(period);
}
examination.setPeriodList(periodList);
}