Tuesday 14 April 2015

CALCULATE DIFFERENCE BETWEEN TWO DATES WITHOUT USING CALENDAR CLASS

/* 
 * Let 1st date be dd/mm/yy 
 * let 2nd date be da/ma/ya
 * Whether latest date is entered first or not it does not make a difference 
 */    
My Input


public class Date
    {
        int date , month , year ;
        Date( int dt , int mn , int yr)// A parameterized Constructor to initialize vslues
        {
            date = dt ;
            month = mn ;
            year = yr ;
        }
        void main(int dd , int mm , int yy , int da , int ma , int ya )//Taking parameterized input
        {
               Date obj = new Date(dd , mm , yy);//creating an object of the first Date
               Date obj2 = new Date(da , ma , ya);//creating an object of the second Date
               int sum = obj.date+obj.month*30+obj.year*365;//calculating total number of days in the 1st date
               int sum2 = obj2.date+obj2.month*30+obj2.year*365;//calculating total number of days in the 2st date
               int diff = sum - sum2 ;//taking difference of the two date in terms of days
               int years = diff/365;
               int rem1 = diff%365 ;
               int months = rem1/30 ;
               int days = rem1%30 ;
               if(days<0)// if the latest date is entered in 2nd Date
               {
                   days = days*-1;
                   months = months*-1;
                   years = years*-1;
                }
               System.out.println("Difference in Days = "+days+" Months = "+months+" Years = "+years);//printing the output
            }
        }
     
My Output

No comments:

Post a Comment