|
|
|
@ -19,5 +19,33 @@ public class IncDate extends Date |
|
|
|
public void addDays(int numDays) |
|
|
|
{ |
|
|
|
// TODO: implement this method |
|
|
|
int days_to_add = numDays; |
|
|
|
int[] MONTHS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
|
|
|
boolean is_leap = (m_year % 400 == 0) || ((m_year % 4 == 0) && !(m_year % 100 == 0)); |
|
|
|
|
|
|
|
for (int current_month = 0; current_month < m_month-1; current_month ++) |
|
|
|
days_to_add += MONTHS[current_month]; |
|
|
|
|
|
|
|
if ((m_month > 2) && is_leap) |
|
|
|
days_to_add += 1; |
|
|
|
days_to_add += m_day-1; |
|
|
|
m_month = 1; |
|
|
|
m_day = 1; |
|
|
|
while (days_to_add > 365) { |
|
|
|
is_leap = (m_year % 400 == 0) || ((m_year % 4 == 0) && !(m_year % 100 == 0)); |
|
|
|
if (is_leap) |
|
|
|
days_to_add -= 1; |
|
|
|
days_to_add -= 365; |
|
|
|
m_year += 1; |
|
|
|
} |
|
|
|
is_leap = (m_year % 400 == 0) || ((m_year % 4 == 0) && !(m_year % 100 == 0)); |
|
|
|
if (is_leap) |
|
|
|
MONTHS[1] +=1; |
|
|
|
|
|
|
|
while (days_to_add >= MONTHS[m_month-1]) { |
|
|
|
days_to_add -= MONTHS[m_month-1]; |
|
|
|
m_month += 1; |
|
|
|
} |
|
|
|
m_day=days_to_add+1; |
|
|
|
} |
|
|
|
} |