Calculate Day using Pure Math Month Table Algorithm JAVA

Equivalent Day are as follows:

1 = Sunday
2 – 6 = Monday – Friday
0 or 7 – Saturday

Code:
public class Main {
    public static void main(String[] args) {
        int month = 11;        //Jan starts at 0
        int year = 2016;    //December 2 2016
        int date = 2;       
        int monthtable[] = {0,3,3,6,1,4,6,2,5,0,3,5};
        final boolean isleap = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
        int x = date + monthtable[month];
        int y = x - (x/7*7);
        int last2inyears = year % 100;
        int z = last2inyears - (last2inyears/28*28);
        int a = last2inyears / 4;
        int b = z + a;
        int first2inyears = (int) (year / 100);
        int e = 5*(first2inyears- (first2inyears/4*4));
        int centtable = (e - (e/7*7));
        int c = b - centtable;
        if((month == 0 || month == 1) && isleap){
            c--;
        }
        int d = c + y;
        int day = d - (d/7*7);
        /*
         * 1 = Sun
         * 2-6 = Mon - Fri
         * 7/0 = Sat
         */
        System.out.println(day);
    }
}
 

Similar threads


Latest posts

Top Bottom