previous method
Returns the previous LocalDate falling on the specified DayOfWeek. This is a strict 'previous' - if this date on already falls on the target day of the week, the returned value will be a week earlier.
targetDayOfWeek
: The ISO day of the week to return the previous date of.
Returns: The previous LocalDate falling on the specified day of the week.
InvalidOperationException
: The underlying calendar doesn't use ISO days of the week.ArgumentOutOfRangeException
:targetDayOfWeek
is not a valid day of the week (Monday to Sunday).
Implementation
LocalDate previous(DayOfWeek targetDayOfWeek)
{
// Avoids boxing...
if (targetDayOfWeek < DayOfWeek.monday || targetDayOfWeek > DayOfWeek.sunday)
{
throw RangeError('targetDayOfWeek');
}
// This will throw the desired exception for calendars with different week systems.
DayOfWeek thisDay = dayOfWeek;
int difference = targetDayOfWeek - thisDay;
if (difference >= 0)
{
difference -= 7;
}
return addDays(difference);
}