| « Cached Out | No Peas for Me, Thanks » |
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.
|
I love the other WTF in the code.. Using the miscellaneous "stop" boolean to detect if the inner for loop has been abandoned, just so that the outer loop can be broken out of! Instead of just using a labelled break.....
|
|
I think there are some date formats missing in the original code... lots of them.
Which reminds me, we need some special kind of torture for people who represent dates such as 07/08/09. |
|
Professionals treat all warnings as errors.
Amateurs ignore warnings. Idiots suppress warnings. |
|
What disturbs me is the parameter name: "datas". Since "data" is already the plural of "datum", what is datas? Some kind of fourth dimensional measurement?
|
Re: Extensive Date Parsing
2008-10-27 16:38
•
by
Mike5
(unregistered)
|
Well, clearly you are NOT an engineer... Br,Mike5 |
| « Cached Out | No Peas for Me, Thanks » |