Skip to content

Instantly share code, notes, and snippets.

@benoitx
Created May 26, 2014 08:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benoitx/4ef7b83eab810d100222 to your computer and use it in GitHub Desktop.
Save benoitx/4ef7b83eab810d100222 to your computer and use it in GitHub Desktop.
Date Calc using JDK8 LocalDate
// create or get the Holidays
final Set<LocalDate> holidays = new HashSet<LocalDate>();
holidays.add(LocalDate.parse("2006-08-28"));
//... keep adding all holidays for 2006
// create the HolidayCalendar ASSUMING that the set covers 2006!
final HolidayCalendar<LocalDate> calendar = new DefaultHolidayCalendar<LocalDate>
(holidays, LocalDate.parse("2006-01-01"), LocalDate.parse("2006-12-31"));
// register the holidays, any calculator with name "UK"
// asked from now on will receive an IMMUTABLE reference to this calendar
LocalDateKitCalculatorsFactory.getDefaultInstance().registerHolidays("UK", calendar);
// ask for a LocalDate calculator for "UK"
// (even if a new set of holidays is registered, this one calculator is not affected
DateCalculator<LocalDate> cal = LocalDateKitCalculatorsFactory.forwardCalculator("UK");
// set startDate, this will also set the current business date.
cal.setStartDate(LocalDate.parse("2006-08-28"));
// startDate stays 28 Aug 06 BUT the currentDate has moved,
// according to Forward handler to 29 Aug 2006.
LocalDate start = cal.getStartDate(); // 28 Aug 06
LocalDate current = cal.getCurrentBusinessDate(); // 29 Aug 06
LocalDate newCurrent = cal.moveByDays(4).getCurrentBusinessDate(); // 4 Sept 06 due to weekend!
// Example with Tenor, 1W with a 2 day spot lag
LocalDate date1WeekFromSpot = cal.moveByTenor(StandardTenor.T_1W, 2).getCurrentBusinessDate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment