Thursday, 4 September 2014

Date Difference

Objective :

The BlueJ Program of class Date calculates the Difference Between The Previous Date and the New Current Date in Days. This is Calculated by Step by Step deviation Method in which the Difference in years is First Calculated and then Months for the next and The Month Before the Set Date. Then, the difference is Calculated and the Sum of the Dates is Presented As Output. The Program has been well Explained using comments about the Function of Each Loop the program executes.
Java Program Source Code :

/**
 * The BlueJ Program of class Date calculates the Difference Between 
 * The Previous Date and the New Current Date.
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
import java.util.*;
public class Date
{
    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)
    {
        Date obj=new Date();
        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);
    }
}

0 comments :

Post a Comment