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!
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.
Fastest Download Speed with UC Browser.
New High Speed Mobile Browser, Free!
Download large files with UC Browser.
Comments