Calculate day of week
Below is a small java program used to calculate which day in week a date is based on the input date. This function is implemented without using any function of Date library.
public class Cal {
public static void main (String args[]) {
int y = 2009;
int m = 9;
int d = 20;
int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= ((m < 3) ? 1 : 0);
System.out.println("Result:" +(y + y/4 - y/100 + y/400 + t[m-1] + d) % 7);
}
}
The output result’ll be a number from [0] to [6] that is corresponding to [Sunday] ~ [Saturday] in a week
Advertisement