Prayagraj
Session 2020-2021
Round 10
Subject : Computer Science
Class : XI
Worksheet 6
Question:
Write a program to accept 2 dates in the string
format dd/mm/yyyy and find the difference in days
between the 2 dates.
Example:
INPUT:
Date 1 : 20/12/2012
Date 2 : 11/02/2013
OUTPUT: Difference = 54 days
The program should include the part for validating
the inputs namely the date and the day on
1st January of that year.
Program Code :
// The class Date_Difference inputs 2 dates and finds
the difference between them
import [Link].*;
class Date_Difference
{
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//function for checking for Leap Year
int isLeap(int y)
{
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
return 29;
else
return 28;
}
//function for checking date validation
boolean dateValidate(int d, int m, int y)
{
month[2]=isLeap(y);
if(m<1 || m>12 || d<1 || d>month[m] || y<1000
|| y>9999)
return false;
else
return true;
}
//function for finding day number from year = 1 till the
inputted year
int dayno(int d, int m, int y)
{
int dn=0;
month[2]=isLeap(y);
for(int i=1;i<m;i++)
{
dn=dn+month[i];
}
dn=dn+d;
for(int i=1000;i<y;i++)
{
if(isLeap(i)==29)
dn=dn+366;
else
dn=dn+365;
}
return dn;
}
public static void main(String args[])
{
Scanner sc= new Scanner([Link]);
Date_Difference ob=new Date_Difference();
[Link]("Enter the 1st date in
(dd/mm/yyyy) format: ");
String date1=[Link]().trim();
int p,q;
//Extracting the day
p=[Link]("/");
int d1=[Link]([Link](0,p));
//Extracting the month
q=[Link]("/");
int m1=[Link]([Link](p+1,q));
//Extracting the year
int y1=[Link]([Link](q+1));
[Link]("Enter the 2nd date in
(dd/mm/yyyy) format: ");
String date2=[Link]().trim();
p=[Link]("/");
int d2=[Link]([Link](0,p));
q=[Link]("/");
int m2=[Link]([Link](p+1,q));
int y2=[Link]([Link](q+1));
//Validating both the dates
if([Link](d1,m1,y1)==true &&
[Link](d2,m2,y2)==true)
{
int a=[Link](d1,m1,y1);
int b=[Link](d2,m2,y2);
[Link]("Output : Difference =
"+[Link](a-b)+" days.");
}
else
[Link]("Invalid Date");
}
}