Design a program which accepts your accepts your dob in dd mm yyyy format. Check whether the date entered is a valid date or not. If it is valid, display "VALID DATE". Also compute and display the day number of the year for the dob And the date in words as given example if it is valid display invalidate and then terminate the program.
EXAMPLE : 1
INPUT : Enter your date in dd mm yyyy format
05
01
2013
OUTPUT :  VALID DATE  5
5th January 2013

EXAMPLE : 2
INPUT : Enter Your DOB in dd mm yyyy
03
04
2010
OUTPUT : VALID DATE 
93
3rd April 2013
            




import java.util.*;
class Validate
{
    int dd,mm,yyyy;
    String day[]={"th","st","nd","rd"};
    String month[]={""," January "," February "," March "," April "," May "," June "," July ",
            " August "," September "," October "," November "," December "};
    int nyear[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int lyear[]={0,31,29,31,30,31,30,31,31,30,31,30,31};

    void input(){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter your date of birth in dd mm yyyy format");
        dd=sc.nextInt();
        mm=sc.nextInt();
        yyyy=sc.nextInt();        
    }

    void date_numbers()
    {
        int n=0;

        // check leap year acc. to that find day on the bases of year
        if(((yyyy%4==0)&&(yyyy%100!=0))||(yyyy%400==0))
        {
            for(int i=1;i< mm;i++)
            {
                n=n+lyear[i];
            }
            n=n+dd;
        }

        //not leap year
        else
        {
            for(int i=1;i< mm;i++)
            {
                n=n+nyear[i];
            }
            n=n+dd;
        }

        System.out.println(n);
    }

    void date_to_words()
    {
        String d=""+dd;
        String year=""+yyyy;
        String s="";
        // here if dd is 1 then 1st,2 then 2nd, 3 then 3rd, after that th is there
        if(dd<=3)
        {
            s=d+day[dd]+month[mm]+year;
        }
        else if(dd>=4){

            if(dd>10)
            {
                int t=dd%10;
                if(t<=3)
                {

                    s=d+day[dd]+month[mm]+year;
                }
                else{
                    s=d+day[0]+month[mm]+year;
                }
            }
            else{
                s=d+day[0]+month[mm]+year;
            }
        }

        System.out.println(s);
    }

    int valid_date()
    {
        int flag=0;

        // checking if that date is between  0 and total day in that month
        if((mm>=1&&mm<=12) && yyyy>=1)
        {

            //leap year check
            if(((yyyy%4==0)&&(yyyy%100!=0))||(yyyy%400==0))
            {
                if(dd>=0&&dd<=lyear[mm])
                {
                    flag=1;
                }
                else
                {
                    flag=0;
                }
            }
            else
            {
                if(dd>=0&&dd<=nyear[mm])
                {

                    flag=1;
                }
                else
                {
                    flag=0;
                }
            }
        }
        else
        {
            flag=0;
        }
        return flag;
    }

    public static void main(String args[])
    {
        Validate obj=new Validate();
        obj.input();
        int c=obj.valid_date();
        if(c==0)
        {
            System.out.println("INVALID DATE");
        }
        else
        {
            System.out.println("VALID DATE");
            obj.date_numbers();
            obj.date_to_words();
        }
    }
}



     
                     

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com