private static void CheckDay(int year, Month month, int day) { int minDay = 1; int maxDay = month.Days(YearUtil.IsLeapYear(year)); if (day < minDay || day > maxDay) { throw new ArgumentOutOfRangeException($"{nameof(day)} must be between {minDay} and {maxDay} for the month {month.Name(new CultureInfo("en-US"))}"); } }
/// <summary> /// Returns a new <see cref="LocalDate" /> with the specified year. /// If the day of month is invalid for the new year, it will be changed to the last valid day of the month. /// </summary> /// <param name="year"></param> /// <returns></returns> public LocalDate WithYear(int year) { CheckYear(year); // adjust February 29 to February 28 if the new year is not a leap year Month month = m_month; int day = m_day; if (IsLeapDay && !YearUtil.IsLeapYear(year)) { day = Month.February.Days(false); } return(new LocalDate(year, month, day)); }
/// <summary> /// Returns a new <see cref="LocalDate" /> with the specified month. /// If the day of month is invalid for the new year, it will be changed to the last valid day of the month. /// </summary> /// <param name="month"></param> /// <returns></returns> public LocalDate WithMonth(Month month) { CheckMonth(month); // adjust to the last valid day of the month, if the new month has fewer days than the old month int year = m_year; int day = m_day; int daysOfMonth = month.Days(YearUtil.IsLeapYear(m_year)); if (daysOfMonth < day) { day = daysOfMonth; } return(new LocalDate(year, month, day)); }