Zeller's Congruence PDF Print E-mail
Written by Charles   
Saturday, 20 September 2008 08:07

This is a Java implementation of 'Zeller's Congruence', an algorithm for calculating the day of the week,
given a date. This is for the Gregorian calendar and the source is HERE:

 

 


import java.util.*;


public class Zeller {
    /**
     * A demo implementation of 'Zeller's Congruence' for the Gregorian Calendar
     * See http://en.wikipedia.org/wiki/Zeller's_congruence
     *
     * @param args (Not used)
     */
    final static String[] DAYS_OF_WEEK = {
            "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday"
        };

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the date in dd/mm/yyyy form: ");

        String[] atoms = input.nextLine().split("/");
        int q = Integer.parseInt(atoms[0]);
        int m = Integer.parseInt(atoms[1]);
        int y = Integer.parseInt(atoms[2]);

        if (m < 3) {
            m += 12;
            y -= 1;
        }

        int k = y % 100;
        int j = y / 100;

        int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
            (5 * j)) % 7;

        System.out.println("That date was a " + DAYS_OF_WEEK[day] + ".");
    }
}
Comments
Search
sohibe   |62.240.36.xxx |2009-04-14 00:58:17
the best code ever tested work 100%
great job and thank you
Charles   |86.136.123.xxx |2009-04-14 11:53:50
Thank you sohibe - nice to be appreciated
Jeff   |67.61.249.xxx |2009-06-10 03:59:56
Thank you for the program. It taught me how to use Zeller's Congruence for a
school project. I gave you credit in the program. I hope that is ok.
Charles   |86.136.72.xxx |2009-06-10 07:25:15
Thanks Jeff - glad i could help
sohibe   |41.254.2.xxx |2009-06-11 03:08:46
man check your algorithm again cuz it gives wrong days like 4-4-2004 or
4-4-2000
try and tell me please
Charles   |81.132.108.xxx |2009-06-11 09:39:02
Thanks sohibe - we should be OK now
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Thursday, 11 June 2009 08:37 )