Find the name of the day on the given date

Find The Name Of The Day On The Given Date

WAP to enter the current date in dd/mm/yyyy format and the day name on the 1st of January of that year. Display the name of the day on the given date.
/* Write a program to accept a date in the string format dd/mm/yyyy
 * and accept the name of the day
 * on 1st of January of the corresponding year. Find the day for the given date.
INPUT Date: 5/7/2001 Day on 1st January : MONDAY
OUTPUT  Day on 5/7/2001 : THURSDAY
Test run the program on the following inputs:
4/9/1998             THURSDAY                     FRIDAY
31/8/1999            FRIDAY                       TUESDAY
6/12/2000            SATURDAY                     WEDNESDAY

 The program should include the part for validating the inputs namely the date
and the day on 1st January of that year.

 import java.util.*;
 class CurrentDayName
  public static void main (String args[])
   throws InputMismatchException
   Scanner scan = new Scanner(;

   int i,dd,mm,yy,tdays, index,r;
   String date,dayName;

   System.out.println("Enter a date in the string format dd/mm/yyyy: ");
        System.out.println("If the date is 9th Februaray,
        2013 enter it as 09/02/2013. ");
    date =;

    // Convert the date in string format to day, month and year
        dd = Integer.parseInt(date.substring(0,2));
        mm = Integer.parseInt(date.substring(3,5));
        yy = Integer.parseInt(date.substring(6));

        System.out.println("Enter the name of the day on 1st of January ");
       dayName =;

        //Store the days of the months
       int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31};

      //If it is a leap year, februrary should have 29 days.
      if(yy%4 == 0)
       dom[1] = 29;

     if(dd < 0 || dd > dom[mm-1]){
      System.out.println("Invalid date.");
       //Store the day names of the week
       String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
              "Friday", "Saturday", "Sunday"};

       //Find the corresponding day of the year

      tdays = 0;
      //Add the days of the month
      for(i = 0; i < mm-1; i++)
         tdays+= dom[i];

   //Add the days of the current month
      tdays+= dd;

    index = 0;
    //Find the index of the entered day name in the day names array
   for(i = 0; i < 7; i++)
       index = i;


  //find the index of the day in the day names array for the current day
     r = tdays%7 + index - 1;

       if( r >= 7)
          r -= 7;

     System.out.println("Day on " + date + " : " + days[r]);
   } // else

  }catch(Exception e){
   System.out.println("Please enter the date in the specified format.");
   System.out.println("Error : "+e);

}//end of main
}//end of class

Have something to say? Log in to comment on this post.