Adjust dates

Submitted by morgdx on Fri, 2006-10-27 12:47.

This recipe shows you how to adjust dates using the four main date adjustment conventions.

Whilst this recipe only shows adjusting a date, you can also adjust whole schedules using the same method, just pass a schedule instead of a date to the HolidayCalendar.adjust method.

For a recipe to create a schedule, click here.


// Create two new dates for
// Saturday 30 September and
// Sunday 1 October
Calendar date1 = ISDADateFormat.parse("2006/9/30");
Calendar date2 = ISDADateFormat.parse("2006/10/1");

// Use the default holiday calendar
// factory to provide us with a
// holiday calendar for weekends
HolidayCalendar weekends =
  HolidayCalendarFactory.newInstance().
  getHolidayCalendar("WE");

// Use the weekends holiday calendar
// to print out date1 and date2
// adjusted

System.out.println(
    ISDADateFormat.format(date1)+
    " PRECEDING "+
    ISDADateFormat.format(
      weekends.adjust(
        date1,
        BusinessDayConvention.PRECEDING
      )
    )
  );
System.out.println(
    ISDADateFormat.format(date1)+
    " FOLLOWING"+
    ISDADateFormat.format(
      weekends.adjust(
        date1,
        BusinessDayConvention.FOLLOWING
      )
    )
  );

System.out.println(
    ISDADateFormat.format(date1)+
    " MODIFIED PRECEDING "+
    ISDADateFormat.format(
      weekends.adjust(
        date1,
        BusinessDayConvention.MODIFIED_PRECEDING
      )
    )
  );

System.out.println(
    ISDADateFormat.format(date1)+
    " MODIFIED FOLLOWING "+
    ISDADateFormat.format(
      weekends.adjust(
        date1,
        BusinessDayConvention.MODIFIED_FOLLOWING
      )
    )
  );

System.out.println(
    ISDADateFormat.format(date2)+
    " PRECEDING "+
    ISDADateFormat.format(
      weekends.adjust(
        date2,
        BusinessDayConvention.PRECEDING
      )
    )
  );
System.out.println(
    ISDADateFormat.format(date2)+
    " FOLLOWING"+
    ISDADateFormat.format(
      weekends.adjust(
        date2,
        BusinessDayConvention.FOLLOWING
      )
    )
  );

System.out.println(
    ISDADateFormat.format(date2)+
    " MODIFIED PRECEDING "+
    ISDADateFormat.format(
      weekends.adjust(
        date2,
        BusinessDayConvention.MODIFIED_PRECEDING
      )
    )
  );

System.out.println(
    ISDADateFormat.format(date2)+
    " MODIFIED FOLLOWING "+
    ISDADateFormat.format(
      weekends.adjust(
        date2,
        BusinessDayConvention.MODIFIED_FOLLOWING
      )
    )
  );

This code will output:

2006/9/30 PRECEDING 2006/9/29
2006/9/30 FOLLOWING 2006/10/2
2006/9/30 MODIFIED PRECEDING 2006/9/29
2006/9/30 MODIFIED FOLLOWING 2006/9/29
2006/10/1 PRECEDING 2006/9/29
2006/10/1 FOLLOWING 2006/10/2
2006/10/1 MODIFIED PRECEDING 2006/10/2
2006/10/1 MODIFIED FOLLOWING 2006/10/2

Click here for the full source code.

Vote Result
Score: 0.0, Votes: 0
email this story

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
More information about formatting options Captcha Image: you will need to recognize the text in it.
Please type in the letters/numbers that are shown in the image above.