##### Introduction :

Hii all, today we are going to make our own calendar.Our focus is to find the weekday of the given date and for this, we are going to use functions.Since to construct a quality program this is our responsibility to first break the program into different parts and then assemble it to make one.

##### Description Of Program :

- First of all, we need to make a strategy to construct a calendar and the strategy follows is to take a reference date i.e. the required date and then standard date i.e. the present date.For this, we calculate the total days between the standard date and the reference date and to execute it properly we make different functions.I’m going to explain the task of all the functions in a series of preferences.
**isleapyear function:**This function will be having a boolean return type, we will call this function to check whether the year is a leap year or not so that we can accurately calculate the number of days between the reference date and the standard date.**comparedates:**This function is having integer return type and as the name suggests this function is to compare the year, month and dates so that we can analyse two dates correctly.**getDaysDifference :**This is the most important function in our code, this function is used to calculate the total number of days between the standard date and the reference date.**getDaysInMonthAndYears :**As we know there are the different number of days in different months so, we are using this function to calculate the total number of days accurately.**checkDates:**This function is throwing an exception so that if the user enters the negative data or the month and days out of range, then it can detect the data and throws exception error as Invalid Data.**getWeekDays :**This is function is having String as return type function, from the function getDaysDifference is used to calculate the total number of days and then in this function, we will find the remainder of the total number of days and hence will pass a switch to take a standard day as reference.**Main Function:**Then we will call the main function to conclude our code calling getWeekDays function and enters the required date.

Hope you find it helpful.

Thank You!

##### CODE :

package exceptions;

import java.util.Scanner;

public class CalenderException {

int d1,d2,y1,y2,t;

int m1,m2;

public CalenderException(){

Scanner s=new Scanner(System.in);

System.out.println(“Enter the standard date i.e. present date”);

d1=s.nextInt();

m1=s.nextInt();

y1=s.nextInt();

System.out.println(“Enter the reference date i.e. required date”);

d2=s.nextInt();

m2=s.nextInt();

y2=s.nextInt();

}

public static int compareDates(int d1,int m1,int y1,int d2,int m2,int y2)

{

//-1 if D1<D2, 0 if D1==D2, 1 if D2>D1

if(y1>y2)

return -1;

else

if(y2>y1)

return 1;

if(m1>m2)

return -1;

else

if(m2>m1)

return 1;

if(d1>d2)

return -1;

else

if(d2>d1)

return 1;

return 0;

}

public static int getDaysDifference(int d1,int m1,int y1,int d2,int m2,int y2) throws Exception

{

//D2>D1

int t;

checkDate(d1, m1, y1);

checkDate(d2, m2, y2);

int cmp=compareDates(d1, m1, y1, d2, m2, y2);

int sign=1;

if(cmp==0)

return 0;

if(cmp<1)

{

sign=-1;

t=y1;

y1=y2;

y2=t;

t=m1;

m1=m2;

m2=t;

t=d1;

d1=d2;

d2=t;

}

int diff1=d1-1;

int diff2=d2-1;

int diff3=0;

for(int i=1;i<=m1-1;i++)

diff3=diff3 + getDaysInMonthAndYears(i, y1);

int diff4=0;

for(int i=1;i<=m2-1;i++)

diff4=diff4 + getDaysInMonthAndYears(i, y2);

int diff5=0;

for(int i=y1;i<=y2-1;i++)

if(isLeapYear(i))

diff5=diff5 + 366;

else

diff5=diff5 + 365;

int diff=diff2 + diff4 + diff5-diff1-diff3;

return sign*diff;

}

public static String getWeekDay(int d,int m,int y) throws Exception{

int days=getDaysDifference(9, 11, 2017,d,m,y);

days=days%7;

if(days<0)

days=days+7;

days=days % 7;

switch(days)

{

case 0:

return “Thursday”;

case 1:

return “Friday”;

case 2:

return “Saturday”;

case 3:

return “Sunday”;

case 4:

return “Monday”;

case 5:

return “Tuesday”;

case 6:

return “Wednesday”;

}

return “Invalid”;

}

public static int getDaysInMonthAndYears(int month,int year)

{

switch(month)

{

case 4:

case 6:

case 9:

case 11:

return 30;

case 2:

if(isLeapYear(year))

return 29;

else

return 28;

default:return 31;

}

}

public static boolean isLeapYear(int year)

{

if(year % 400==0)

return true;

if(year % 4==0 && year % 100!=0)

return true;

return false;

}

public static void checkDate(int d,int m,int y) throws Exception

{

if(y<=0)

throw new Exception(“Invalid Year”);

if(m<1 || m>12)

throw new Exception(“Invalid month”);

int days=getDaysInMonthAndYears(m, y);

if(d<1 || d>days)

throw new Exception(“Invalid date”);

}

public static void sop(Object ex){

System.out.println(ex);

}

public static void main(String[] args) {

try{

String day=getWeekDay(22,3,1997);

System.out.println(day);

}

catch(Exception ex)

{

sop(ex);

}

}

}

##### OUTPUT :