If I had a dollar for every time I've seen someone doing custom date and time parsing, I'd have somewhere between eight and ten dollars. It's just not something you see much of these days. All major programming languages have built-in support for parsing dates, times, currency, etc., and programmers are pretty good about using them.

When I have to parse a date, I take the simplest approach I can – something like Date.Parse(dateString). This is because I am a programmer and not an engineer. An engineer on Benjamin B.'s team uses a different approach. It works incredibly well! It says so right in the snippet below!

/**
 * <pre>
 * This function find a date into given datas and
 * return it as a Calendar object.
 * The parsing is done with deprecated new Date(String s)
 * constructor which works incredibly well.
 * </pre>
 * @param datas Where the date must be found.
 * 
 * @return The date which was found.
 */
@SuppressWarnings("deprecation")
public static Calendar getDate(String datas)
{
    Date d=null;
    Calendar result=Calendar.getInstance();
    //Try standard date
    try
    {
        d= new Date(datas);
    }
    //Try various formats for all available locales (languages)
    catch(Exception e)
    {
        //http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
        String[] formats={"EEE, d MMM yyyy HH:mm:ss Z",
                      "d.m.y H:m",
                      "EEE, d. MMM yyy HH:mm",
                      "E, d. M y HH:mm",
                      "EEE, MMM dd, yyyy 'at' HH:mm a"
                      };

        boolean stop=false;

        //Each format
        for(String format:formats)
        {
            //Each locale
            for(Locale locale:Locale.getAvailableLocales())
            {                
                try
                {
                    SimpleDateFormat df= new SimpleDateFormat(format,locale);
                    d=df.parse(datas);

                    //Found , stop search
                    stop=true;
                    break;
                }
                catch(Exception f)
                {

                }
            }
            if(stop)
                break;
        }

    }

    if(d==null)
        return null;

    result.setTime(d);
    return result;
}

Those <pre>s in there are part of the comments, and I'm not sure why they're in there. The application in question is only used in one locale, so Ben provided an alternate solution:

Date date = null;
for(String format:formats)
{
    try
    {
        DateFormat df = new SimpleDateFormat('A Format', Locale.ENGLISH);
        date = df.parse(rawTimestamp);
    }
    catch (ParseException e)
    {
        // Timestamp is not parsable. The format is not good.
    }
}

Tsk tsk, Ben. With your tendency to build simple solutions, you'll never make it as an engineer.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!