Tuesday , 26 March 2019
Home » Class 12 » Java Program for finding Future Date

Java Program for finding Future Date

Question:

Write a program to accept a date in the string format dd/mm/yyyy. Check whether the date entered is valid or not. If it is valid, then input a certain number of days. Then calculate and print the future date after adding the given number of days if the future date is valid. If the date entered is invalid, then display a proper error message.

Example:    

INPUT:

Date     :  07 / 04 / 2013
Enter Number of days after : 7

OUTPUT:

Entered Date : 07 / 04 / 2013
Future Date  : 14 / 04 / 2013

Programming Code:

/**
* The class FutureDate inputs a date and finds the future date after some given days
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class FutureDate
{
public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        System.out.print("Enter the date in (dd/mm/yyyy) format: ");
        String date=br.readLine().trim();
        int p,q,count=0;
        p=date.indexOf("/");
        int d=Integer.parseInt(date.substring(0,p));
        q=date.lastIndexOf("/");
        int m=Integer.parseInt(date.substring(p+1,q));
        int y=Integer.parseInt(date.substring(q+1));
        System.out.println("Entered Date: "+date);

        if((y%400==0) || ((y%100!=0)&&(y%4==0))) // Checking for leap year
        month[2]=29;

        if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999) // Performing Date Validation
        {
            System.out.println("Invalid Date");
        }
        else
        {
            System.out.print("Enter number of days after which future date is to be found: ");
            int days=Integer.parseInt(br.readLine());
            while(count<days)
            {
                d++;
                count++;
                
                /* If day exceeds the maximum days of a month then day should start from 1
                   and month should increase */

                if(d>month[m])
                {
                    d=1;
                    m++;
                }
                
                /* If month exceeds 12 then month should start from 1
                   and year should increase */
                if(m>12)
                {
                    m=1;
                    y++;
                    if((y%400==0) || ((y%100!=0)&&(y%4==0)))
                        month[2]=29;
                    else
                        month[2]=28;
                }
            }
            System.out.println("Future Date : "+d+"/"+m+"/"+y);
        }
    }
}

Output:

Enter the date in (dd/mm/yyyy) format: 18/02/2014
Entered Date: 18/02/2014
Enter number of days after which future date is to be found: 2
Future Date : 20/2/2014

Check Also

Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.

Leave a Reply

Your email address will not be published. Required fields are marked *