a litle help on how to convert date to day

i have an assignment in which a user enters a date in dd/mm/yy format and the program must output what day of the week that date is.

I use 3 different variables to get the values of dd,mm and yy

Code :

Scanner scanner = new Scanner(System.in);
System.out.println("ENTER THE DAY. NOTE :PLEASE DONT ADD '0' IF THE DAY IS SINGLE DIGIT LIKE 1 TO 9");
int day = scanner.nextInt();
System.out.println("ENTER THE MONTH NOTE :PLEASE DONT ADD '0' IF THE MONTH IS SINGLE DIGIT LIKE 1 TO 9");
int month = scanner.nextInt();
System.out.println("ENTER THE YEAR");
int year = scanner.nextInt();

I am not allowed to use the calendar util. only basic things like switch, scanner,for, while etc.

i know how to find if a year is a leap year or not, but what should i do next?? can someone please help me on this

Re: a litle help on how to convert date to day

You will need at least 1 date of reference and which day of the year it is. Then simply calculate how many days there are between that reference date and the date the user entered (you will have to use the rules for how many days there are in a month, when a leap year is, etc.). Once you have the number of days in between, simply take the modulus of 7 from that date. This will give you how many days forward you need to advance.

Ex.:

Reference date was a Sunday, March 14, 2010 (pi day!). The number of days between reference date and the date the user entered was 704 (I'll leave calculating this up to you). 704 % 7 = 4. So, 4 days after Sunday would be Monday, Tuesday, Wednesday, **Thursday**.

Re: a litle help on how to convert date to day

Quote:

Originally Posted by

**helloworld922**
You will need at least 1 date of reference and which day of the year it is. Then simply calculate how many days there are between that reference date and the date the user entered (you will have to use the rules for how many days there are in a month, when a leap year is, etc.). Once you have the number of days in between, simply take the modulus of 7 from that date. This will give you how many days forward you need to advance.

Ex.:

Reference date was a Sunday, March 14, 2010 (pi day!). The number of days between reference date and the date the user entered was 704 (I'll leave calculating this up to you). 704 % 7 = 4. So, 4 days after Sunday would be Monday, Tuesday, Wednesday, **Thursday**.

so to check if i understood:

lets say my base date is 1/3/1900 (1900 was not a leap year)

and the user gives me for example 10/2/2000 ( it is a leap year so february has 29)

user year-base year=2000-1900=100 years after.

base date = 1/3>> 365-59=306 ( since my start date is marxh 1st 1900 : (february+january)-365,)

if is leap year= 366-(days before the given date)?

given date=10/2 >>>>> (31+10)=41

>>>>>>>>>>>>>>>> (number of days after 1/1/2000)

365*(number of years after -1) +306=36441+41

if i do 36372%7 =5, the nit means it is a thursday??

Re: a litle help on how to convert date to day

Remember, there could be a ton of leap years in between the reference date and the date you entered. For every year that is a leap year in between the two dates you will have to add an extra day. I think the basic idea looks correct, though. You also must know what day March 1, 1900 is (It's a Thursday, fyi).

One more thing to keep in mind: If the user enters a date before your reference date, I believe you will have to travel **backwards** through the days. I believe the modulus operator will return the negative remainder if you use it on a negative number.

ex.: (-494) % 7 = -4

If your reference date happened to be a Wednesday, the user's date would be Tuesday, Monday, Sunday, **Saturday**.

Re: a litle help on how to convert date to day

i have done it like this but i am having trouble with leap years. when i insert a year which is not leap it works fine, but when the year is leap it prints the wrong day. can you point me to the right way @-) ?

Code :

import java.util.Scanner;
public class assex6 {
public static void main(String[] args) {
int base=1900,bdate=1,bmonth=3,year=0,days=0,ad=0,feb=0,calculate=0,finalday=0,i=0,sum=0,year2=0;
boolean leap;
Scanner scanner = new Scanner(System.in);
System.out.println("ENTER THE DAY. NOTE :PLEASE DONT ADD '0' IF THE DAY IS SINGLE DIGIT LIKE 1 TO 9");
int dd = scanner.nextInt();
System.out.println("ENTER THE MONTH NOTE :PLEASE DONT ADD '0' IF THE MONTH IS SINGLE DIGIT LIKE 1 TO 9");
int mm = scanner.nextInt();
System.out.println("ENTER THE YEAR");
int yy = scanner.nextInt();
while ((yy>2100)|(yy<1900)) {
System.out.println("give a year from 1900-2100");
yy = scanner.nextInt();
}
//check if year is leap or not:
if (yy % 4 == 0) {
// Is theYear Divisible by 4 but not 100?
if (yy % 100 != 0) {
leap=true;
//System.out.println(yy + " is a leap year.");
}
// Is theYear Divisible by 4 and 100 and 400?
else if (yy % 400 == 0) {
leap=true;
//System.out.println(yy + " is a leap year.");
}
// It is Divisible by 4 and 100 but not 400!
else {
leap=false;
//System.out.println(yy + " is not a leap year.");
}
}
// It is not divisible by 4.
else {
leap=false;
//System.out.println(yy + " is not a leap year.");
}
if (leap==true) {
days=366;
feb=29;}
else {
days=365;
feb=28;
}
year2= (int)((yy-base)/4);
year=(yy-base);
switch(mm) {
case 1:
ad = 0;
break;
case 2:
ad = (31+dd);
break;
case 3:
ad = (31+feb+dd);
break;
case 4:
ad = (31+feb+31+dd);
break;
case 5:
ad = (31+feb+31+30+dd);
break;
case 6:
ad = (31+feb+31+30+31+dd);
break;
case 7:
ad = (31+feb+31+30+31+30+dd);
break;
case 8:
ad = (31+feb+31+30+31+30+31+dd);
break;
case 9:
ad = (31+feb+31+30+31+30+31+31+dd);
break;
case 10:
ad = (31+feb+31+30+31+30+31+31+30+dd);
break;
case 11:
ad = (31+feb+31+30+31+30+31+31+30+31+dd);
break;
case 12:
ad = (31+feb+31+30+31+30+31+31+30+31+30+dd);
break;
}
/*if ((ad<60)&(leap==true)){
calculate=(365*(year-1)+306+ad+year2-2);}
else */calculate=(365*(year-1)+306+ad+year2-1);
finalday=(calculate%7);
switch(finalday) {
case 1:
System.out.println("friday");
break;
case 2:
System.out.println("saturday");
break;
case 3:
System.out.println("sunday");
break;
case 4:
System.out.println("monday");
break;
case 5:
System.out.println("tuesday");
break;
case 6:
System.out.println("wendesday");
break;
case 0:
System.out.println("thursday");
break;
}
}
}

Re: a litle help on how to convert date to day

to be more precise, it works fine for every date except for the leap year 1916. all other years work perfectly. i cant seem to find why though......

i can solve it partially if i put
Code :

if (yy==1916) finalday=(calculate+3%7);

but that is not the best solution right??

by partially i mean that if you insert 23/3/1916 the output is sunday. but for any othe month of the year it shifts one day before, i mean eg : 23/5/1916 is saturday yet the out put of the program is friday.....

i am sure there must be a better one inside the loops or in the calculations but i cant seem to find it...

Re: a litle help on how to convert date to day

mmm... messy :P

When you're comparing to see if the year is divisible by 400, you're using an else if, but it should be just an if.