Sarah Mathews
Sarah Mathews

Reputation: 21

Setting a fixed date and counting the number of days to an input birthdate to determing day of week - Java

My task is such - I need to set a specific date - 01/01/1913, which is a wednesday. I then need the user to input their birthdate. The program will then calculate which day of the week the person was born in. We are not allowed to use the gregorian calendar to do it for us, we are required to input the algorithm ourselves.

So far, I have the input set up,

public class FindDay4Birthdate 
{
    public static void main(String[] args) 
    {
        // declare variables
        String bbday = "";
        String bbmonth = "";
        String bbyear = "";
        int bday;
        int bmonth;
        int byear;

        String daysList[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter your date of birth - ");
        sc.useDelimiter("[-/.\\s]");
        if (sc.hasNext()); {
            bbday = sc.next();
            bbmonth = sc.next();
            bbyear=sc.next();
            bday = Integer.parseInt(bbday);
            bmonth = Integer.parseInt(bbmonth);
            byear = Integer.parseInt(bbyear);
        } // end if statement
    }
}

I am not sure where to go from here. Any help on how to take the next step would be appreciated. I know that I need to use mod 7, but do not know how, or where, I should use it.

Upvotes: 0

Views: 462

Answers (4)

rolfl
rolfl

Reputation: 17707

what you may be looking for is Zeller's Congruence

Upvotes: 1

Java Devil
Java Devil

Reputation: 10959

If you are allowed to use the Calendar class (but probably not), do the following

Calendar birthday = Calendar.getInstance();
birthday.set(byear,bmonth - 1,bday);

//Create your time 1/1/1913
Calendar startDate = Calendar.getInstance();
startDate.set(1913,0,1);

int numberOfDays = 0;

//Loop counting days
while(startDate.before(birthday))
{
    numberOfdays++;
    startDate.add(Calendar.DATE, 1);
}

//Get index for your days array
int index = ((numberOfDays%7)+3)%7;
String day = daysList[index]; 

Upvotes: 0

Jason Braucht
Jason Braucht

Reputation: 2368

Is there any particular algorithm you are supposed to implement? If so, could you update your question with it so we can help you with the Java implementation?

If you are struggling with the algorithm itself, take a look at Wikipedia's Determination of the day of the week for a quite thorough explanation of various ways to calculate the day of week.

The formula given in the section titled Basic method for mental calculation the formula is (d + m + y + floor(y\4) + c) mod 7 where

  • d is the day of the month,
  • m is the month's number in the months table,
  • y is the last two digits of the year
  • c is the century number. For a Gregorian date, this is 6 if the first two digits of the year are evenly divisible by 4, and subsequent centuries are 4-2-0 (so the century numbers for 2000, 2100, 2200, and 2300 are respectively 6, 4, 2, and 0). For a Julian date, this is 6 for 1200, and subsequent centuries subtract 1 until 0, when the next century is 6 (so 1300 is 5, and 1100 is 0).

If the result is 0, the date was a Sunday; if 1 it was a Monday, and so on...

Upvotes: 1

nachokk
nachokk

Reputation: 14413

1) Count the days between from 01/01/1913 to user input, count also +1 day for leap-years

2) The result of 1) %7 , will give you the remainder, if is 0 then it's Wednesday, if not you have to count 1= Thursday 2=Friday .etc...

Upvotes: 1

Related Questions