Day of Year

Page 1 of 1

4 Replies - 4767 Views - Last Post: 03 April 2012 - 07:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273636&amp;s=76b19d99da4f48407c6dfd63a178a1be&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 blackjnr

Reputation: 0
• Posts: 13
• Joined: 19-March 11

Day of Year

Posted 03 April 2012 - 04:40 AM

Got an assignment due tomorrow, most of it is done, except i can't figure out one bit.
This is the question:
Write a class called DayOfYear that prompts the user to enter month, day
and year as integer values, and calculates the day of a year corresponding
to a speci ed date. (For example, if month is 2, day is 29 and year is 2000
then day of the year is 60). The program should perform a validation on
entered day, month and year, and if, the date is valid, calculates the day
of the year. (For example, if year is less or equal 0 then the program prints
that the year is invalid, and etc.) The program also checks for a leap year
and adds extra day if necessary.

The only thing i can't figure out is the algorithm to use for calculating the day of the year.
So far i've managed to get this far:

```int total = 0;
int i;
for ( i = 1 ; i < month; i++)
total += days;
total += day+i;
int DOY = total;
```

If a user entered 11/11/2011, the answer should be 315, but the above algorithm gives 322.

This is algorithm for the leap year, and the switch statements for the different number of days in a month
```		//Algothrim to calculate leap year
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);

//Number of days in a month
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 2:
if (isLeapYear)
days = 29;
else
days = 28;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
}

```

Anyone know the algorithm i need?

Is This A Good Question/Topic? 0

Replies To: Day of Year

#2 Sinned

Reputation: 19
• Posts: 208
• Joined: 13-October 10

Re: Day of Year

Posted 03 April 2012 - 05:44 AM

This could help:
```	private int dayOfYear(int year, int month, int day) // 0<month<=12
{
short[] daysInMonth = new short[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
short[] daysInMonthLeap = new short[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);

int yearDay = 0;
for(int i=0; i<month-1; i++)
{
if(isLeapYear) yearDay += daysInMonthLeap[i];
else yearDay += daysInMonth[i];
}
yearDay+=day;
return yearDay;
}
```

#3 blackjnr

Reputation: 0
• Posts: 13
• Joined: 19-March 11

Re: Day of Year

Posted 03 April 2012 - 03:16 PM

Thank you very much sir!!!

#4 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

Re: Day of Year

Posted 03 April 2012 - 06:27 PM

I would simplify it a bit

```private int dayOfYear(int year, int month, int day) // 0<month<=12
{
short[] daysInMonth = new short[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
daysInMonth[1] = 29;

int yearDay = 0;
for(int i=0; i<month-1; i++)
{
yearDay += daysInMonth[i];
}
yearDay+=day;
return yearDay;
}

```

#5 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

Re: Day of Year

Posted 03 April 2012 - 07:00 PM

I'd simplify it a bit more

```private int dayOfYear(int year, int month, int day) // 0<month<=12
{
short[] monthDays = new short[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

int yearDay = monthDays[month-1] + day;

if(((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)) && month > 2)
yearDay++;

return yearDay;
}

```

edit: disabled leap day adjustment for jan and feb

This post has been edited by r.stiltskin: 03 April 2012 - 07:38 PM