Improved schedule generation with future codes
Improved futureCode parsing (without adding 2000), throw self-explaining
exception if futureCode has wrong format, corrected documentation
@@ -12,6 +12,7 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
@@ -631,13 +632,10 @@ public class ScheduleGenerator {
}
/**
- * Schedule generation with futureCodes. Futures are assumed to expire on the third wednesday in the respective month.
- * See other constructor for parameter description.
- *
- *
+ * Schedule generation with futureCodes (in the format DEC17). Futures are assumed to expire on the third wednesday in the respective month.
*
* @param referenceDate The date which is used in the schedule to internally convert dates to doubles, i.e., the date where t=0.
- * @param futureCode Future code, e.g. DEC2017
+ * @param futureCode Future code in the format DEC17
* @param startOffsetString The start date as an offset from the spotDate (build from tradeDate and spotOffsetDays) entered as a code like 1D, 1W, 1M, 2M, 3M, 1Y, etc.
* @param maturityString The end date of the last period entered as a code like 1D, 1W, 1M, 2M, 3M, 1Y, etc.
* @param frequency The frequency (as String).
@@ -663,11 +661,16 @@ public class ScheduleGenerator {
int paymentOffsetDays
)
{
- int futureExpiryYears = 2000 + Integer.parseInt(futureCode.substring(futureCode.length()-2));
+ int futureExpiryYearsShort = Integer.parseInt(futureCode.substring(futureCode.length()-2));
String futureExpiryMonthsString = futureCode.substring(0,futureCode.length()-2);
- DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd/MMM/yyyy").toFormatter(Locale.ENGLISH);
- String futureExpiryString = "01/" + futureExpiryMonthsString + "/" + futureExpiryYears;
- LocalDate futureExpiryDate = LocalDate.parse(futureExpiryString, formatter);
+ DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd/MMM/yy").toFormatter(Locale.ENGLISH);
+ String futureExpiryString = "01/" + futureExpiryMonthsString + "/" + futureExpiryYearsShort;
+ LocalDate futureExpiryDate;
+ try{
+ futureExpiryDate = LocalDate.parse(futureExpiryString, formatter);
+ } catch(DateTimeParseException e) {
+ throw new IllegalArgumentException("Error when parsing futureCode " + futureCode + ". Must be of format MMMYY with english month format (e.g. DEC17)");
+ }
// get third wednesday in month, adjust with following if no busday
while(!futureExpiryDate.getDayOfWeek().equals(DayOfWeek.WEDNESDAY))
futureExpiryDate = futureExpiryDate.plusDays(1);
