Worksheet 5
Worksheet 5
Session 2020-2021
Round 10
Class : XI
Worksheet 5
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:
import java.util.*;
class FutureDate
{
public static void main(String args[])
{
Scanner sc= new Scanner(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=sc.nextLine().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(d>month[m])
{
d=1;
m++;
}