Pages

Wednesday 22 August 2012

JAVA:Convert String Date into Date Object

/****************************************************************
    * Desc : It convert the given String to Date object based on the given format.
    * @return java.util.Date
 ***************************************************************/
    public static java.util.Date stringToDate(String userDate, String format)
    {
        if (userDate != null)
        {
            DateFormat df = new SimpleDateFormat(format);   
            try {
                java.util.Date date = df.parse(userDate);
                return date;
            }
            catch (ParseException pe) {
                pe.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }