# Thread: Calculate the day of the week for a particular date

1. Member
Join Date
Feb 2011
Posts
13
Rep Power
0

## Calculate the day of the week for a particular date

I have to write this program to show what day of week you were born i wrote this code but i don't know what i have to do after :( :mad::confused:

• You will be using the JOptionPane object and the .showInputDialog method to prompt for user input. Prompt the user for a String object that represents a date in the mm/dd/yyyy format such as: 04/24/1982.

public class DayName
{

public static void main (String[]args){
// These are integer valuse
int iMonth=0;
int iDay=0;
int iYear=0;
int iCenturyYear =0;

// These are string values
String sDay;
String sMonth;
String sYear;
String sCenturyYear;

// Let the user to input their Date of Birth
sDay = JOptionPane.showInputDialog("Enter the day of your Birthday: ");
sMonth = JOptionPane.showInputDialog("Enter the month of your birthday: ");
sYear = JOptionPane.showInputDialog("Enter the month of your birthday: ");

// Convert string to integers
iMonth = Integer.parseInt(sMonth);
iDay = Integer.parseInt(sDay);
iYear = Integer.parseInt(sYear);
iCenturyYear=Integer.parseInt(sCenturyYear);

JOptionPane.showInputDialog("Enter the birth date (mm/dd/yyyy): ");

}// end main
}// end of the DayName class

2. Are you allowed to use the java date class? If so I believe this can help you accomplish the task, try googling the java date API, it may be date format.

3. Member
Join Date
Feb 2011
Posts
13
Rep Power
0
Technical Approach:
You can use the Java if samples on Blackboard as a basis or the examples from the book.

• You will be using the JOptionPane object and the .showInputDialog method to prompt for user input. Prompt the user for a String object that represents a date in the mm/dd/yyyy format such as: 04/24/1982.
• Use the String object’s methods to extract out these values and convert them to primitive integers:
o iDay (in our example it would be 24)
o iMonth (in our example it would be 04)
o iCenturyYear (in our example it would be 1982)
o iYear (in our example it would be 82)
• Write the if logic to determine if the year is a leap year. This can be accomplished as follows:
o If the year is divisible by 4 with no remainder and the year is divisible by 100
o Or the year is divisible by 400 with no remainder.
o Use a Boolean called bIsLeapYear to indicate if the years in a leap year.
• Write a Java if…else if logic to determine the Centuries Code for the iCenturyYear variable. Use the table below to find the value, in our example 1982 would be 0:
o 1700–1799 4
o 1800–1899 2
o 1900–1999 0
o 2000–2099 6
o 2100–2199 4
o 2200–2299 2
o 2300–2399 0
o 2400–2499 6
o 2500–2599 4
o 2600–2699 2

• Write a Java if…else if logic to determine the Months Code for the iMonth variable. Use the table below to find the value, in our example 04 would be 6:
o January 0 (if a leap year use 6 use bIsLeapYear variable to check)
o February 3 (if a leap year 2 use bIsLeapYear variable to check)
o March 3
o April 6
o May 1
o June 4
o July 6
o August 2
o September 5
o October 0
o November 3
o December 5
• Once you have extracted the different values of the date and you have determined the Centuries Code and the Month Code do this calculation:
o The Centuries Code for 11/24/1982 should be: 0
o Note the last two digits of the iYear variable: 82
o Divide the 82 by 4: 82/4 = 20.5 and drop the fractional part: 20
o The Month Code for 04 in the iMonth variable should be: 6
o Add all numbers from steps 1–4 to the day of the month iDay (in this case, 24): 0+82+20+6+24=132.
o Modulus divide the sum from step 5 by 7 and find the remainder: 132/7=18 remainder 6
o Find the remainder in the days name table below using a Java switch statement: 6=Saturday:
 0 Sunday
 1 Monday
 2 Tuesday
 3 Wednesday
 4 Thursday
 5 Friday
 6 Saturday
• Once you have determined the day of week use the JOptionPane object’s. showMessageDialog method to show the date that was inputted and the Day Name you computed.
• Make sure to use Hungarian notation and ample comments. Your code should contain your name, date, Programming Assignment 6 and some general comments as to what the purpose of the code is.
• Upload the Java source code and the compiled class to blackboard by the due date.

Testing Plan:
Date Result
4/24/1982 Saturday
09/18/1783 Thursday
06/08/1998 Monday
06/19/2054 Friday
02/29/1996 Thursday

This is the whole thing that he gave us

4. Alright, it is pretty confusing if you read it all at once but just re read it a few times. The questions gives you a step by step on how to do it, just follow that description precisely. Read the questions a few times and get as deep an understanding as you possibly can.

5. Member
Join Date
Feb 2011
Posts
13
Rep Power
0
I have been trying it for so long if i could of i would of done it

6. Well you are not trying very hard.

Your instructions say to get user input in the format of mm/dd/YYY. That is ONE input and yet you have this:
Java Code:
```// Let the user to input their Date of Birth
sDay = JOptionPane.showInputDialog("Enter the day of your Birthday: ");
sMonth = JOptionPane.showInputDialog("Enter the month of your birthday: ");
sYear = JOptionPane.showInputDialog("Enter the month of your birthday: ");```
The instructions spell it out very comprehensively in a step by step manner. Simply follow the steps. Do not move onto the next step until you have coded and fully tested each step.

7. Member
Join Date
Feb 2011
Posts
13
Rep Power
0
// This is what i have done

import javax.swing.*;
import java.util.Calendar;
import java.util.Date;

public class DayName
{

public static void main (String[]args){
// These are integer valuse
int iMonth=0;
int iDay=0;
int iYear=0;
int iCenturyYear =0;
int icentCode = 0;
int iMonCode = 0;

// These are string values
String sDay = "";
String sMonth = "";
String sYear = "";
String sCenturyYear = "";

// This is a boolean value
boolean bIsLeapYear = false;

// Let the user to input their Date of Birth
String sGetDate = JOptionPane.showInputDialog("Enter the birth date in the format (mm/dd/yyyy): ");

// Convert string to integers

int icount = 0;

for (int n = 0; n < 2; n++)
{

sDay += sGetDate.charAt(n);

icount++;

}//end for

for (int n = 3; n < 5; n++)
{

sMonth += sGetDate.charAt(n);

icount++;

}//end for

for (int n = 8; n < 10; n++)
{

sYear += sGetDate.charAt(n);

icount++;

}//end for

for (int n = 6; n < 10; n++)
{

sCenturyYear += sGetDate.charAt(n);

icount++;

}//end for

//converting user input without slash '/' to interger
iDay = Integer.parseInt(sDay);
iMonth = Integer.parseInt(sMonth);
iCenturyYear = Integer.parseInt(sCenturyYear);
iYear = Integer.parseInt(sYear);

/*if()
{
boolean bIsLeapYear =
}*/

if (iCenturyYear >= 1700 || iCenturyYear <= 1799)
{
icentCode = 4;
}//end iCenturyYear range 1700 - 1799
else if (iCenturyYear >= 1800 || iCenturyYear <= 1899)
{
icentCode = 2;
}//end iCenturyYear range 1800 - 1899
else if (iCenturyYear >= 1900 || iCenturyYear <= 1999)
{
icentCode = 0;
}//end iCenturyYear range 1900 - 1999
else if (iCenturyYear >= 2000 || iCenturyYear <= 2099)
{
icentCode = 2;
}//end iCenturyYear range 2000 - 2099
else if (iCenturyYear >= 2100 || iCenturyYear <= 2199)
{
icentCode = 6;
}//end iCenturyYear range 2100 - 2199
else if (iCenturyYear >= 2200 || iCenturyYear <= 2299)
{
icentCode = 4;
}//end iCenturyYear range 2200 - 2299
else if (iCenturyYear >= 2300 || iCenturyYear <= 2399)
{
icentCode = 2;
}//end iCenturyYear range 2300 - 2399
else if (iCenturyYear >= 2400 || iCenturyYear <= 2499)
{
icentCode = 0;
}//end iCenturyYear range 2400 - 2499
else if (iCenturyYear >= 2500 || iCenturyYear <= 2599)
{
icentCode = 6;
}//end iCenturyYear range 2500 - 2599
else if (iCenturyYear >= 2600 || iCenturyYear <= 2699)
{
icentCode = 4;
}//end iCenturyYear range 2600 - 2699

if (iMonth == January)
{
iMonCode = 0;
}//end January

if (iMonth == february)
{
iMonCode = 3;
}//end February

JOptionPane.showMessageDialog(null, sDay + " " + sMonth + " " + sCenturyYear + " " + sYear,"Get Date is: GetDate", JOptionPane.INFORMATION_MESSAGE);

}// end main
}// end of the DayName class

\

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•