How do I convert a String date to a util date object?

Many a time we need to convert Java String Object to Java Date Object. Every programmer expectation is for the Date Class to have constructor which takes String Object as input and convert that into a Date Object - but that is not the case as the date involves formatting. We need to use a Date formatter to describe the output format of the date. It's annoying and even the most experienced programmer's google for the sample code and paste it.

This example shows you how to convert a String date to a util date object.
package net.javaiq.examples.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 * This class demonstrates on how to convert a String date to a util date object. 
 * @author JavaIQ.net
 */
public class StringDateToUtilDateConverter {

    public static Date convertStringDateToUtilDate(String strDate) {
        Date date = null;

        if (strDate != null) {
            try {
                DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
                date = formatter.parse(strDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return date;
    }

    /**
     * Method to test other methods in the class with sample inputs
     * @param args
     */
    public static void main(String[] args) {
        String date = "12-10-2010";
        Date convertedDate = convertStringDateToUtilDate(date);
        System.out.println("convertedDate : " + convertedDate);
    }
}