Find the day of your date of birth in java

Posted By: Java Examples - 11:27 AM

Share

& Comment

Here are the steps :
Step 1
  • read date of birth as three numbers dd, mm, yyyy (year in 4 digits)
  • count the number of days, td,  from 1.1.1900 to dd.mm.yyyy (including both dates)
    •  For example, number of days from 1.1.1900 to 2.2.1900 is 33, (that is td=33).
  •  divide td by 7 and get the remainder R ( if td=33 then R=5 )
  •  Decide the day depending on the value of R:
    •    if R=0 then day is Sunday
    •    if R=1 then day is Monday and so on....
    •    if R=6 then day is Saturday
    •  (For example, 2.3.1900 was a Friday (1900 was not a leapyear)  
Step 2

  •  can be done easily as follows (keeping in mind that td is eventually divided by 7  and the remainder only matters)

    •  find  dy= yyyy-1900
    • find ly = (dy-1)/4 (taking the quotient and  forgetting the remainder)
    • find the magic number, mn, corresponding to the month mm from the table:
    •  Month(mm)       1    2    3    4    5    6    7    8    9    10    11    12
    •  MagicNumber(mn) 0    3    3   -1    1   -3   -1    2   -2     0     3    -2
    •  find td=dd+dy+ly+mn
    • if mm>2 and yyyy is a leapyear then td=td+1
    • find R=td%7

Java version of the Program is given below :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dayofbirth;

/**
 *
 * @author ACHCHUTHAN
 */
import java.util.Scanner;

public class MyDay {

    public int date, month, year, day;

    public MyDay() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your Date of Birth. Like DD/MM/YY Formet");
        System.out.println(" Date in two digits (DD)");
        date = input.nextInt();
        System.out.println(" Month in two digits (MM)");
        month = input.nextInt();
        System.out.println(" Year in four digits (YYYY)");
        year = input.nextInt();
    }

    public int getDate() {
        return date;
    }

    public int getDay() {
        return day;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    public static boolean isLeapYear(int yy) {
        if (yy % 100 == 0) {
            return yy % 400 == 0;
        } else {
            return yy % 4 == 0;
        }
    }

    public static int getMagicNumber(int m) {
        int mn = -1;
        switch (m) {
            case 2:
            case 3:
            case 11:
                mn = 3;
                break;
            case 1:
            case 10:
                mn = 0;
                break;
            case 6:
                mn = -3;
                break;
            case 8:
                mn = 2;
                break;
            case 9:
            case 12:
                mn = -2;
                break;
        }
        return mn;
    }

    public static String getDay(int R) {
        if (R == 0) {
            return "Sunday";
        } else if (R == 1) {
            return "Monday";
        } else if (R == 2) {
            return "Tuesday";
        } else if (R == 3) {
            return "Wenesday";
        } else if (R == 4) {
            return "Thurssday";
        } else if (R == 5) {
            return "Friday";
        }
        return "Saterday";
    }

    public String FindDay() {
        int R, td, dy, ly, dd, mn;
        dy = year - 1900;
        ly = (dy - 1) / 4;
        mn = getMagicNumber(month);
        dd = date;
        td = dd + dy + ly + mn;
        if (month > 2 && isLeapYear(year)) {
            td = td + 1;
            R = td % 7;

            return getDay(R);
        } else {
            R = td % 7;
            return getDay(R);
        }

    }

    public static void main(String args[]) {
        MyDay D = new MyDay();
        System.out.println("Your entered date " + D.getDate() + "/"
         + D.getMonth() + "/" + D.getYear() + ".That day is " + D.FindDay());
    }
}

Output of this program :
run:
Enter your Date of Birth. Like DD/MM/YY Formet
 Date in two digits (DD)
 29
 Month in two digits (MM)
 01
 Year in four digits (YYYY)
 2012
Your entered date 29/1/2012.That day is Sunday
BUILD SUCCESSFUL (total time: 13 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .