Validate String Date in Java Example, Convert date format dd/MM/yyyy to dd/MMM/yyyy

All applications / programs accepts Date input as a string. Now the real concern is wheather the accepted date is valid or not. To check the given date is valid or not, you can either write your own logic for  parsing and validate or you can use built-in class  SimpleDateFormat for parsing.

One way is that you have to manually parse the date and validate, this is very tedious job. The following conditions should be checked.
Suppose you are accepting the date in the format “dd/MM/yyyy”, the length of the Date String should be 10, it should have two slashes (/) , the slash positions in the date will be 3 and 6 . other characters except slashes (/) in the date should be numeric, not characters. Finally you have to validate the day, month & year. For this you have to split the Day, Month, Day from the date string. Then you have to validate the day based on the month & year. For example 29/02/2005 is not a valid date because the month is february & the year is not a leap year. Sample code for Manual parse & validation is as follows

public static boolean isValidDateString(String thedate) {
if (dateString == null) return false;
int length = dateString.length();
if (length != 10) return false;
for (int pos = 0; pos <length; pos++)
if (pos == 2 || pos == 5)
{
if (dateString.charAt(i) != '/') return false;
}
else if (!Character.isDigit(dateString.charAt(pos))) return false;
...............
................
return true;
}

Now the easiest way to check the given date is valid or not, is to use the built-in method parse(dateString) of the SimpleDateFormat class. Assume that our program accepts Date String & validation format (dd/MM/yyyy, dd/MMM/yyyy, yyyy-MM-dd , etc) through command line. (note : MM should be in uppercase). Now let us see How the parse method works? it parses the date and converts the given date into valid date using the given format and returns in the same format . For Example : Input is 32/08/2011 dd/MM/yyyy. This is not a valid date. So the output of pasrse method will be 01/09/2011. ie. it roll forwards to next valid date. Another Example : Input String is 25/13/2011 dd/MM/yyyy, output will be : 25/01/2012. if the Input is 25/12/2011, then output will be 25/12/2011.  So if the input date string is invalid , the output will not be matched with the input string, otherwise both (input , output) will be equal.This is the interesting thing to validate the date. The program also converts the given date to dd/MMM/yyyy (i.e. 25/Dec/2011)

import java.util.Calendar;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
class StringDateValidation
{
public static void main (String args[])
{
if (args.length!=2)
{
System.out.println("Error in Input . Try in the format . example : java DateValidation 25/09/2011 dd/MM/yyyy ");
return;
}
System.out.println("Given date is :" + args[0]);
System.out.println(" ");
boolean flag=isValidDateString(args[0], args[1]);
if (flag==true) System.out.println("Given date is valid"); else System.out.println("Given date is invalid");
}


public static boolean isValidDateString(String dateString, String dateFormat)
{
SimpleDateFormat out = new SimpleDateFormat("dd/MMM/yyyy");
SimpleDateFormat in = new SimpleDateFormat(dateFormat);
Date parseDate = null;
try
{
parseDate = in.parse(dateString);
System.out.println("Date after Parse in the format (dd/MMM/yyyy) : " +out.format(parseDate));
System.out.println(" ");
}
catch (ParseException e)
{ return false; }
if (!in.format(parseDate).equals(dateString)) return false;
return true;
}
}

OUTPUT :

Given date is :31/08/2011

Date after Parse in the format (dd/MMM/yyyy) : 31/Aug/2011

Given date is valid

2) java StringDateValidation 32/08/2011 dd/MM/yyyy

Given date is :32/08/2011

Date after Parse in the format (dd/MMM/yyyy) : 01/Sep/2011

Given date is invalid

3) java StringDateValidation 2000-02-29 yyyy-MM-dd

Given date is :2000-02-29

Date after Parse in the frmat (dd/MMM/yyyy) : 29/Feb/2000

Given date is valid

You may also like

Leave a Reply

Be the First to Comment!