TradingWeekImpl extends JodaWorkingWeek implements TradingWeek { @Override public DateTime getPreviousSessionDate(DateTime dt, Period period) { if(dt == null) { return null; } int interval = period.size(); PeriodType periodType = period.getPeriodType(); dt = dt.millisOfSecond().withMinimumValue(); TradingSession tradingSession = getTradingSessionOnOrBefore(dt); boolean skip = false; switch(periodType) { case YEAR: { dt = dt.minusYears(interval).monthOfYear().withMaximumValue(); dt = dt.dayOfMonth().withMaximumValue(); skip = true; } case QUARTER: { if(!skip) { LocalTime lt = dt.toLocalTime(); dt = ExtendedChronology.withPeriodEnd(dt).minusMonths(3 * interval).dayOfMonth().withMaximumValue(); dt = dt.withHourOfDay(lt.getHourOfDay()).withMinuteOfHour(lt.getMinuteOfHour()). withSecondOfMinute(lt.getSecondOfMinute()); skip = true; } } case MONTH: { if(!skip) { dt = dt.minusMonths(interval); dt = dt.dayOfMonth().withMaximumValue(); skip = true; } } case WEEK: { if(!skip) { dt = dt.minusWeeks(interval); dt = dt.withDayOfWeek(getEndSession().day()); skip = true; } } case DAY: { if(periodType != PeriodType.WEEK) { dt = skip ? dt : dt.minusDays(interval); tradingSession = getTradingSessionOnOrBefore(dt); if(tradingSession.day() > dt.getDayOfWeek()) { dt = dt.minusDays((DateTimeConstants.SUNDAY - dt.getDayOfWeek()) + 1); } dt = dt.withDayOfWeek(tradingSession.day()); skip = true; } } case HOUR: { if(skip) { LocalTime lt = tradingSession.end(); dt = dt.withHourOfDay(lt.getHourOfDay()); }else{ dt = dt.minusHours(interval); tradingSession = getTradingSessionOnOrBefore(dt); if(!tradingSession.contains(dt)) { if(tradingSession.day() > dt.getDayOfWeek()) { dt = dt.minusDays((DateTimeConstants.SUNDAY - dt.getDayOfWeek()) + 1); } dt = dt.withDayOfWeek(tradingSession.day()); dt = dt.withHourOfDay(tradingSession.end().getHourOfDay()); } skip = true; } } case MINUTE: { if(skip) { LocalTime lt = tradingSession.end(); dt = dt.withMinuteOfHour(lt.getMinuteOfHour()); }else{ dt = dt.minusMinutes(interval); tradingSession = getTradingSessionOnOrBefore(dt); if(!tradingSession.contains(dt)) { if(tradingSession.day() > dt.getDayOfWeek()) { dt = dt.minusDays((DateTimeConstants.SUNDAY - dt.getDayOfWeek()) + 1); } dt = dt.withDayOfWeek(tradingSession.day()); LocalTime lt = tradingSession.end(); dt = dt.withHourOfDay(lt.getHourOfDay()).withMinuteOfHour(lt.getMinuteOfHour()); } skip = true; } } case SECOND: { if(skip) { dt = dt.withSecondOfMinute(0); }else{ dt = dt.minusSeconds(interval); tradingSession = getTradingSessionOnOrBefore(dt); if(!tradingSession.contains(dt)) { if(tradingSession.day() > dt.getDayOfWeek()) { dt = dt.plusDays((DateTimeConstants.SUNDAY - dt.getDayOfWeek()) + 1); } dt = dt.withDayOfWeek(tradingSession.day()); LocalTime lt = tradingSession.end(); dt = dt.withHourOfDay(lt.getHourOfDay()).withMinuteOfHour(lt.getMinuteOfHour()); dt = dt.withSecondOfMinute(0); } } calculator.setStartDate(dt.toLocalDate()); LocalDate ldt = calculator.getCurrentBusinessDate(); if(!ldt.isEqual(dt.toLocalDate())) { tradingSession = getTradingSessionOnOrBefore(ldt.toDateTime(dt.toLocalTime())); if(tradingSession.day() > dt.getDayOfWeek()) { dt = dt.minusDays((DateTimeConstants.SUNDAY - dt.getDayOfWeek()) + 1); } dt = dt.withDayOfWeek(tradingSession.day()); LocalTime lt = tradingSession.end(); dt = dt.withHourOfDay(lt.getHourOfDay()).withMinuteOfHour(lt.getMinuteOfHour()).withSecondOfMinute(0); } break; } case TICK: { if(!tradingSession.contains(dt)) { tradingSession = getTradingSessionOnOrBefore(dt); if(tradingSession.day() > dt.getDayOfWeek()) { dt = dt.plusDays((DateTimeConstants.SUNDAY - dt.getDayOfWeek()) + 1); } dt = dt.withDayOfWeek(tradingSession.day()); LocalTime lt = tradingSession.end(); dt = dt.withHourOfDay(lt.getHourOfDay()).withMinuteOfHour(lt.getMinuteOfHour()); dt = dt.withSecondOfMinute(lt.getSecondOfMinute()).withMillisOfSecond(0); } } } return dt; } private  TradingWeekImpl(byte tradingDays); static Builder configBuilder(
        String propertiesFilePath, String calendarName, String tradingWeekName); static Builder configBuilder(
        Properties properties, String calendarName, String tradingWeekName); @Override int length(); @Override void addTradingSession(TradingSession session); @Override TradingSession getStartSession(); @Override TradingSession getEndSession(); @Override int getTradingDaysInMonth(DateTime dt); @Override boolean isTradingDay(DateTime dt); @Override DateTime getSessionTradingDate(DateTime dt); @Override long getWeekMillis(); @Override long getSessionMillisBetween(DateTime dt1, DateTime dt2); TradingSession getTradingSessionOnOrBefore(DateTime date); @Override DateTime getPreviousSessionDate(DateTime dt, Period period); @Override TradingSession getTradingSessionOnOrAfter(DateTime date); @Override DateTime getNextSessionDate(DateTime dt, Period period); static TradingWeek loadDefaultTradingWeek(); @Override boolean isWorkingDay(LocalDate date); static int naturalDaysBetween(int startDay, int endDay); static String dayToString(int day); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); static final TradingWeek DEFAULT; }
@Test public void testGetPreviousSessionDate() { DateTime testDate = new DateTime(2009, 5, 5, 0, 0, 0, 999); TradingWeek tradingWeek = getTestTradingWeek(TEST3); Period tu = new Period(PeriodType.DAY, 1); DateTime previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 4, 0, 0, 0, 999); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 3, 0, 0, 0, 999); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 2, 0, 0, 0, 999); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 1, 0, 0, 0, 999); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-29T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 1, 12, 0, 0, 999); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); tradingWeek = getTestTradingWeek(TEST); testDate = new DateTime(2009, 5, 1, 12, 0, 0, 999); tu = new Period(PeriodType.YEAR, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2008-12-31T15:30:00.000", noZone(previousDate.toString())); tu = new Period(PeriodType.QUARTER, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-03-31T15:30:00.000", noZone(previousDate.toString())); tu = new Period(PeriodType.MONTH, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); tu = new Period(PeriodType.WEEK, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-25T15:30:00.000", noZone(previousDate.toString())); tu = new Period(PeriodType.DAY, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 1, 8, 30, 0, 999); tu = new Period(PeriodType.HOUR, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 1, 9, 30, 0, 999); tu = new Period(PeriodType.HOUR, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-05-01T08:30:00.000", noZone(previousDate.toString())); tu = new Period(PeriodType.MINUTE, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-05-01T09:29:00.000", noZone(previousDate.toString())); tu = new Period(PeriodType.SECOND, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-05-01T09:29:59.000", noZone(previousDate.toString())); testDate = new DateTime(2009, 5, 1, 12, 0, 0, 999); tradingWeek = getTestTradingWeek(TEST3); tu = new Period(PeriodType.DAY, 1); previousDate = tradingWeek.getPreviousSessionDate(testDate, tu); assertEquals("2009-04-30T15:30:00.000", noZone(previousDate.toString())); }