Skip to main content

Java Program to accept any date and print current date and check that the date excepted by the user is valid or not

The following Code is to check whether the date entered by the user is a valid date or not. Within the Code, 'Date()' class is only used to get the current system date and the other things're done by simple logic.
Code is as follow:-

import java.util.*;
public class DateValidation{
    Scanner scan=new Scanner(System.in);
    String mon, da, yea, day3, day4, month3, month4, year7, year8, year9, year10, year11, year12, year5, year6;
    char month1, month2, day1, day2, year1, year2, year3, year4, date2;
    String date;
    int month, day, year;
    public void input(){
        try{
            System.out.println("Enter the date in dd/mm/yyyy format:- ");
            date=scan.nextLine();
        }
        catch(Exception e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
    }

    public void compute(){
        date();
        valid();
    }

    public void date(){
        Date date=new Date();
        System.out.println("Today is "+date);
    }

    public void valid(){
        dateValues();
        validation();
    }

    public void dateValues(){
        day1=date.charAt(0);
        day2=date.charAt(1);
        month1=date.charAt(3);
        month2=date.charAt(4);
        year1=date.charAt(6);
        year2=date.charAt(7);
        year3=date.charAt(8);
        year4=date.charAt(9);
        day3=Character.toString(day1);
        day4=Character.toString(day2);
        da=day3.concat(day4);
        month3=Character.toString(month1);
        month4=Character.toString(month2);
        mon=month3.concat(month4);
        year7=Character.toString(year1);
        year8=Character.toString(year2);
        year9=Character.toString(year3);
        year10=Character.toString(year4);
        year5=year7.concat(year8);
        year6=year9.concat(year10);
        yea=year5.concat(year6);
        System.out.println("Date you input: "+da+"/"+mon+"/"+yea);
        day=Integer.parseInt(da);
        month=Integer.parseInt(mon);
        year=Integer.parseInt(yea);
    }

    public void validation(){
        System.out.print("\n"+day+"/"+month+"/"+year+" is a ");
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            if(day>31)
                System.out.print("Invalid Date!");
            else
                System.out.print("Valid Date!");
        }
        else if(month==4||month==6||month==9||month==11){
            if(day>30)
                System.out.print("Invalid Date!");
            else
                System.out.print("Valid Date!");
        }
        else if(month==2&&year%4==0){
            if(day>29)
                System.out.print("Invalid Date!");
            else
                System.out.print("Valid Date!");
        }
        else if(month==2&&year%2!=0){
            if(day>28)
                System.out.print("Invalid Date!");
            else
                System.out.print("Valid Date!");
        }
        else if(month>12)
            System.out.print("Invalid Date!");
        else if(month<0||day<0||year<0)
            System.out.print("Invalid Date!");
    }

    public static void main(String[] args){
        DateValidation obj=new DateValidation();
        obj.input();
        obj.compute();
    }
}

Output is like:-
Enter the date in dd/mm/yyyy format:-
08-12-1997
Today is Thu Mar 28 00:04:46 IST 2013
Date you input: 08/12/1997
8/12/1997 is a Valid Date!

Still Using Opera? Try Faster UC Browser. [Download Now]
Fastest Download Speed with UC Browser.
New High Speed Mobile Browser, Free!
Download large files with UC Browser.

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...