import java.util.*;
public class DateDifference
{
static Scanner sc=new Scanner(System.in);
static int day,month,year,dd,mm,yyyy;
static int Nleap[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //NON-LEAP YEAR
static int leap[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; //LEAP YEAR
public void setDate()
{
System.out.print("\fEnter The Fixed Date (DD/MM/YYYY) : "); day=sc.nextInt();
System.out.print("\fEnter The Fixed Date (DD/MM/YYYY) : "+day+"/"); month=sc.nextInt();
System.out.print("\fEnter The Fixed Date (DD/MM/YYYY) : "+day+"/"+month+"/"); year=sc.nextInt();
}
private boolean isLeapYear(int year)
{
if(year%400==0||year%4==0&&year%100==0)
return true;
else
return false;
}
public int calculate(int dd,int mm,int yyyy)
{
int DaysCount=0; //TOTAL NO. OF DAYS
// CALCULATE NO. OF DAYS FROM THE NEXT YEAR TILL PREV OF CURRENT YEAR
// INPUTS : 1994 to 2011 ; CALCULATES FROM 1st JAN 1995 to 1st JAN 2011
for(int i=year+1;i<yyyy;i++)
if(isLeapYear(i)) DaysCount+=366;
else DaysCount+=365;
// CALCULATES NO. OF DAYS FROM 1st OF NEXT MONTH TILL THE END OF YEAR
for(int i=month+1;i<=12;i++)
if(isLeapYear(year)) DaysCount+=leap[i];
else DaysCount+= Nleap[i];
// CALCULATES NO. OF DAYS FROM THE DAY TO END OF THE MONTH
if(isLeapYear(year))
for(int i=day;i<=leap[month];i++) DaysCount++;
else
for(int i=day;i<=Nleap[month];i++) DaysCount++;
// CALCULATES NO. OF DAYS FROM 1st JAN OF GIVEN MONTH TILL PREV OF GIVEN MONTH
for(int i=1;i<mm;i++)
if(isLeapYear(yyyy))
DaysCount+=leap[i];
else
DaysCount+=Nleap[i];
// CALCULATES NO. OF DAYS FROM 1st OF GIVEN MONTH TO GIVEN DAY
if(isLeapYear(yyyy))
for(int i=1;i<=dd;i++)
DaysCount++;
else
for(int i=1;i<=dd;i++)
DaysCount++;
// CALCULATES NO. OF DAYS IF SET YEAR = GIVEN YEAR
if(yyyy==year)
if(isLeapYear(yyyy)) DaysCount-=366;
else DaysCount-=365;
return DaysCount;
}
public void setCurrent()
{
System.out.print("\fEnter The Current Date (DD/MM/YYYY) : "); dd=sc.nextInt();
System.out.print("\fEnter The Current Date (DD/MM/YYYY) : "+dd+"/"); mm=sc.nextInt();
System.out.print("\fEnter The Current Date (DD/MM/YYYY) : "+dd+"/"+mm+"/"); yyyy=sc.nextInt();
}
public static void main(String[]args)
{
DateDifference obj=new DateDifference();
obj.setDate();
obj.setCurrent();
int total=obj.calculate(dd,mm,yyyy);
System.out.println("\fFixed Date (DD/MM/YYYY) : "+day+"/"+month+"/"+year);
System.out.println("Current Date (DD/MM/YYYY) : "+dd+"/"+mm+"/"+yyyy);
System.out.println("\nTotal No Of Days : "+total);
}
}
public class DateDifference
{
static Scanner sc=new Scanner(System.in);
static int day,month,year,dd,mm,yyyy;
static int Nleap[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //NON-LEAP YEAR
static int leap[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; //LEAP YEAR
public void setDate()
{
System.out.print("\fEnter The Fixed Date (DD/MM/YYYY) : "); day=sc.nextInt();
System.out.print("\fEnter The Fixed Date (DD/MM/YYYY) : "+day+"/"); month=sc.nextInt();
System.out.print("\fEnter The Fixed Date (DD/MM/YYYY) : "+day+"/"+month+"/"); year=sc.nextInt();
}
private boolean isLeapYear(int year)
{
if(year%400==0||year%4==0&&year%100==0)
return true;
else
return false;
}
public int calculate(int dd,int mm,int yyyy)
{
int DaysCount=0; //TOTAL NO. OF DAYS
// CALCULATE NO. OF DAYS FROM THE NEXT YEAR TILL PREV OF CURRENT YEAR
// INPUTS : 1994 to 2011 ; CALCULATES FROM 1st JAN 1995 to 1st JAN 2011
for(int i=year+1;i<yyyy;i++)
if(isLeapYear(i)) DaysCount+=366;
else DaysCount+=365;
// CALCULATES NO. OF DAYS FROM 1st OF NEXT MONTH TILL THE END OF YEAR
for(int i=month+1;i<=12;i++)
if(isLeapYear(year)) DaysCount+=leap[i];
else DaysCount+= Nleap[i];
// CALCULATES NO. OF DAYS FROM THE DAY TO END OF THE MONTH
if(isLeapYear(year))
for(int i=day;i<=leap[month];i++) DaysCount++;
else
for(int i=day;i<=Nleap[month];i++) DaysCount++;
// CALCULATES NO. OF DAYS FROM 1st JAN OF GIVEN MONTH TILL PREV OF GIVEN MONTH
for(int i=1;i<mm;i++)
if(isLeapYear(yyyy))
DaysCount+=leap[i];
else
DaysCount+=Nleap[i];
// CALCULATES NO. OF DAYS FROM 1st OF GIVEN MONTH TO GIVEN DAY
if(isLeapYear(yyyy))
for(int i=1;i<=dd;i++)
DaysCount++;
else
for(int i=1;i<=dd;i++)
DaysCount++;
// CALCULATES NO. OF DAYS IF SET YEAR = GIVEN YEAR
if(yyyy==year)
if(isLeapYear(yyyy)) DaysCount-=366;
else DaysCount-=365;
return DaysCount;
}
public void setCurrent()
{
System.out.print("\fEnter The Current Date (DD/MM/YYYY) : "); dd=sc.nextInt();
System.out.print("\fEnter The Current Date (DD/MM/YYYY) : "+dd+"/"); mm=sc.nextInt();
System.out.print("\fEnter The Current Date (DD/MM/YYYY) : "+dd+"/"+mm+"/"); yyyy=sc.nextInt();
}
public static void main(String[]args)
{
DateDifference obj=new DateDifference();
obj.setDate();
obj.setCurrent();
int total=obj.calculate(dd,mm,yyyy);
System.out.println("\fFixed Date (DD/MM/YYYY) : "+day+"/"+month+"/"+year);
System.out.println("Current Date (DD/MM/YYYY) : "+dd+"/"+mm+"/"+yyyy);
System.out.println("\nTotal No Of Days : "+total);
}
}
No comments:
Post a Comment