Need explanation of the code
Hello ! I need help as to what the code below does for each of its statement, when dd/mm/yyyy format is enter, it should give 20 Dec 2010 is a Monday:
Code :
public static String dayOfWeek(int dd, int mm, int yyyy) {
int[] monthIndices = {1,4,4,0,2,5,0,3,6,1,4,6};
String[] dayOfWeek = {"Saturday", "Sunday", "Monday",
"Tuesday", "Wednesday", "Thursday",
"Friday"};
if ((yyyy % 4 == 0) && !(yyyy % 100 == 0) ||
(yyyy % 400 == 0)){
monthIndices[0] = 0;
monthIndices[1] = 3;
}
int yy = yyyy % 100;
int yyDiv4 = yy / 4;
int keyNumber = monthIndices[mm-1];
int sum = yy+yyDiv4+keyNumber+dd;
int adjValue = 0;
if (yyyy / 100 == 18)
adjValue = 2;
else
if (yyyy / 100 == 20)
adjValue = 6;
else
if (yyyy / 100 == 21)
adjValue = 4;
int remainder = (sum + adjValue) % 7;
String day = dayOfWeek[remainder];
return day;
}
}
Re: Need explanation of the code
If you really don't know what any of the program lines do, you need to go back to basics.
The Java™ Tutorials
If you do understand some of it, ask a more specific question.
How To Ask Questions The Smart Way
db
Re: Need explanation of the code
@db: even if you've a good knowledge of java, you need some experience to figure out that the following statement
Code :
if ((yyyy % 4 == 0) && !(yyyy % 100 == 0) ||
(yyyy % 400 == 0))
is used to calculate if a year is a leap year.
Re: Need explanation of the code
thanks for reply
have doubts as to why we need to do the code as to:
if year modules 400 is 0
then is_leap_year
else if year modules 100 is 0
then not_leap_year
else if year modules 4 is 0
then is_leap_year