Find past date

Find Past Date


Enter date of submission, number of days given to complete the project and find the date of announcement of project.
/* 
 *  Enter date of submission, number of days given to complete
 *  the project and find the date of announcement of project.
 */

    import java.util.*;
    class DateOfAnnouncement
    {
    public static void main (String args[])throws InputMismatchException
    {
    Scanner scan = new Scanner(System.in);

    int dd, mm, yy, nod,i,tdays = 0,m;

    System.out.println("Enter the day, month and year of the date of announcement of the project: ");
        dd = scan.nextInt();
        mm = scan.nextInt();
        yy = scan.nextInt();

    System.out.println("Enter the no of days given to complete the project: ");
        nod = scan.nextInt();

        int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31};

        // If it is a leap year, februrary should have 29 days.
        if(yy%4 == 0)
            dom[1] = 29;

        // Add the days of the month
            for(i = 0; i< mm-1; i++)
            tdays+= dom[i];

        // Add the days of the current month
         tdays+= dd;

        // Subtract the number of days given to complete the project from total days
        tdays-= nod;

        // Now convert the total calculated days to date

        // If the result is negative that means the project was announced
        // in the previous year
        if(tdays < 0)
        {
            yy--;

            // If the previous year was a leap year, add 366 to get the corresponding
            // day of that year else add 365

            if(yy%4 == 0)
                tdays += 366;
            else
                tdays += 365;
        }
        if(yy%4 == 0)
            dom[1] = 29;
        else
            dom[1] = 28;
        m=0;

        while(tdays>dom[m]){
            tdays-= dom[m];
            m++;
        }
        System.out.println("Date of announcement: " + tdays + "/" + (m+1) + "/" + yy);

    }//end of main
    }//end of class

Have something to say? Log in to comment on this post.

0 comments