Hi, need help as to what is the code doing, if possible each statement if anyone knows:
When it is run, when dd/mm/yyyy format is enter, it will give 20 Dec 2010 is a Monday
Why is the monthIndices and division done for ?

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;
			}
		}

Recommended Answers

All 3 Replies

Why don't you ask the person whose code you copied? It is obvious that it returns the day.

If you want a code that returns the day then write your own and post it here with questions in order to get help. You can use the java.text.SimpleDateFormat class, if you are allowed.

It looks like the author of the code you found is doing something slightly different from this algorithm, but only slightly. This might help you understand what the code is doing.

ok, this is from my lecturer for the exam revision but his lesson has ended i can email to him but it would be easy to ask in a forum of discussion.

Thanks for the info, i'll read up, hope it helps my exam :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.