Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

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

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by helloworld922 View Post
    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??

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.
    Last edited by helloworld922; March 18th, 2010 at 11:25 AM.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ?

    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;
     
    }
     
     
     
     
     }
    }
    Last edited by helloworld922; March 18th, 2010 at 03:19 PM.

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
     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...
    Last edited by george; March 18th, 2010 at 04:33 PM.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    mmm... messy

    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.

Similar Threads

  1. [SOLVED] how to get each field of a current date
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 24th, 2010, 06:33 AM
  2. unablissues wit type Date
    By cysquatch1 in forum Object Oriented Programming
    Replies: 2
    Last Post: December 21st, 2009, 12:20 PM
  3. How to set expiry date for cms?
    By khodam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2009, 01:15 PM
  4. [SOLVED] how to get phone time and date
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: August 26th, 2009, 11:15 AM
  5. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM