Tuesday, November 18, 2014

Validate Date against given Date Format

How to validate given Date against Date Format.

SO there are two approaches,

1.) Using SimpleDateFormat

2.) Using Regex.


Basic Example.

SimpleDateFormat 
 
Date date = null;
    boolean checkformat;
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/dd/MM");
        dateFormat.setLenient(false);
        date = dateFormat.parse("2013/25/09");
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
    if (date == null) {
        checkformat = false;
    } else {
        checkformat = true;
    }

    System.out.println(checkformat);
 
 
Regex based  
 
public static void main(String[] args) throws ParseException {
        String input = "2014/12/31";
        boolean checkformat;
        if (input.matches("([0-9]{4})/([0-9]{2})/([0-9]{2})")) // for yyyy/MM/dd format
            checkformat = true;
        else
            checkformat = false;

        System.out.println(checkformat);
    } 
 
Play around the {2}, {2}, {4} values inside curly braces to prepare regex.
 
Complexity Increases, let say i have many Date Formats.
 
 
public class DateUtil {
 
    // List of all date formats that we want to parse.
    // Add your own format here.
    private static List<SimpleDateFormat>;
            dateFormats = new ArrayList<SimpleDateFormat>() {{
            add(new SimpleDateFormat("M/dd/yyyy"));
            add(new SimpleDateFormat("dd.M.yyyy"));
            add(new SimpleDateFormat("M/dd/yyyy hh:mm:ss a"));
            add(new SimpleDateFormat("dd.M.yyyy hh:mm:ss a"));
            add(new SimpleDateFormat("dd.MMM.yyyy"));
            add(new SimpleDateFormat("dd-MMM-yyyy"));
        }
    };
 
   /**
     * Convert String with various formats into java.util.Date
     *
     * @param input
     *            Date as a string
     * @return java.util.Date object if input string is parsed
     *          successfully else returns null
     */
 
 
public static Date convertToDate(String input) {
        Date date = null;
        if(null == input) {
            return null;
        }
        for (SimpleDateFormat format : dateFormats) {
            try {
                format.setLenient(false);
                date = format.parse(input);
            } catch (ParseException e) {
                //Shhh.. try other formats
            }
            if (date != null) {
                break;
            }
        }
 
        return date;
    }
 


// Test class to test out
public class TestDateUtil {
 
    public static void main(String[] args) {
        System.out.println("10/14/2012" + " = " + DateUtil.convertToDate("10/14/2012"));
        System.out.println("10-Jan-2012" + " = " + DateUtil.convertToDate("10-Jan-2012"));
        System.out.println("01.03.2002" + " = " + DateUtil.convertToDate("01.03.2002"));
        System.out.println("12/03/2010" + " = " + DateUtil.convertToDate("12/03/2010"));
        System.out.println("19.Feb.2011" + " = " + DateUtil.convertToDate("19.Feb.2011" ));
        System.out.println("4/20/2012" + " = " + DateUtil.convertToDate("4/20/2012"));
        System.out.println("some string" + " = " + DateUtil.convertToDate("some string"));
        System.out.println("123456" + " = " + DateUtil.convertToDate("123456"));
        System.out.println("null" + " = " + DateUtil.convertToDate(null));
 
    }  
}
 

No comments:

Post a Comment