Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:36:23+00:00 2026-06-12T00:36:23+00:00

First off, I know that you guys dislike it when questions like these are

  • 0

First off, I know that you guys dislike it when questions like these are asked however I’m unable to see what’s wrong with my code and would like someone to look it over. With that said, I know my code looks terrible and that’s because this has to be all done in one method.

public static int dayOfWeek(int year, int month, int day) {
    int monthCode = 0, centuryCode = 0, numLeapYears = 0, dayOfWeek = 0;

    int yearDigits = year % 100;

    if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
        numLeapYears = yearDigits / 2 + 1;
    }else {
        numLeapYears = yearDigits / 2;
    }

    if(((year >= 1400) && (year <= 1499)) || ((year >= 1800) && (year <= 1899)) || ((year >= 2200) && (year <= 2299))) {
        centuryCode = 2;
    }else if(((year >= 1500) && (year <= 1599)) || ((year >= 1900) && (year <= 1999)) || ((year >= 2300) && (year <= 2399))) {
        centuryCode = 0;
    }else if(((year >= 1600) && (year <= 1699)) || ((year >= 2000) && (year <= 2099)) || ((year >= 2400) && (year <= 2499))) {
        centuryCode = 5;
    }else if(((year >= 1700) && (year <= 1799)) || ((year >= 2100) && (year <= 2199)) || ((year >= 2500) && (year <= 2599))) {
        centuryCode = 4;
    }

    switch(month) {
        case 1: monthCode = 0;
        break;

        case 2: monthCode = 3;
        break;

        case 3: monthCode = 3;
        break;

        case 4: monthCode = 6;
        break;

        case 5: monthCode = 1;
        break;

        case 6: monthCode = 4;
        break;

        case 7: monthCode = 6;
        break;

        case 8: monthCode = 2;
        break;

        case 9: monthCode = 5;
        break;

        case 10: monthCode = 0;
        break;

        case 11: monthCode = 3;
        break;

        case 12: monthCode = 5;
        break;
    }

    dayOfWeek = ((centuryCode + yearDigits + numLeapYears + monthCode + day) % 7);
    return dayOfWeek;
}//dayOfWeek

I haven’t bothered to comment my code yet, the purpose of this function is to calculate the date of which a day falls on (Sunday, Monday, etc.) represented by a number (0, 1, 2, etc.) My code works, however it doesn’t output the correct number. For instance 7/20/1969 is supposed to come out as 0, but my code outputs 3. I can’t seem to find the flaw in my logic for this method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:36:24+00:00Added an answer on June 12, 2026 at 12:36 am

    Your switch on the month looks off – calendar indexes months starting at 0. Therefore cal.get(Calendar.MONTH) == 0 for dates in January.

    With that said, you’ve provided no explanation for what monthCode should represent, which makes it really hard to offer any type of help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I know this is a duplicate question that is asked on here
First off, I know that there are similar questions on SO, but I have
First off, I'm embarassed that I don't know this. I know these things work,
First off, I would like to make clear, that I am SUPER NEW TO
First off: I know that this isn't reliable for actually checking if I can
First off, I know that this question begs the other question - should the
First off, I know I can copy "this" on instantiation, but that doesn't work
First off, I know there are ways to make it so that text can
I know first off that this is probably the worst looking regex ever but
First off, I read all of the suggested questions that sounded halfway relevant and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.