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);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

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

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...