Friday, June 19, 2009

Date to String & viceversa

Yet another simple utility:: date to string and string to date conversion.

Date to String:
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
            format.setLenient(false); //if the string is not inthe expected format:: throws Exception.
            Date date = (Date)o;
            return format.format(date);

String to Date
            if(str ==null )
                return null;
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
            format.setLenient(false);
            Date d = format.parse(str);
            return d;





No comments:

Post a Comment