Pete Freitag Pete Freitag

Advanced Date Parsing with ColdFusion

coldfusion

Have you ever tried to parse a date that ColdFusion didn't recognize? It can be pretty tricky, and usually requires regular expressions.

Suppose you want to use DateFormat on an RFC-822 date. These dates are used by the HTTP protocol, and in RSS feeds. An RFC-822 date looks something like this: Wed, 31 May 2006 14:33:52 GMT. You can pass this date into the ParseDateTime function, and it will return a date that you can format with DateFormat, but it ignores the timezone. So if I were to pass a date like: Wed, 31 May 2006 10:33:52 EST the ParseDateTime function would not treat those two strings as the same date, even though they are equivalent.

Now you could parse out the timezone from the date string, but there is an easier way. Take a look at the SimpleDateFormat java class. You simply initialize this object with a date pattern (see the javadocs for date mask characters), and call the parse - it returns a java.util.Date object (which is what ColdFusion dates are).

In this code example I am using the GetHttpTimeString function to generate an RFC822 date string:

<cfset date = GetHttpTimeString()>
<cfset formatter = CreateObject("java", "java.text.SimpleDateFormat")>
<cfset formatter.init("EEE, dd MMM yyyy HH:mm:ss Z")>
<cfset parsePosition = CreateObject("java", "java.text.ParsePosition")>
<cfset parsePosition.init(0)>
<cfset newDate = formatter.parse(date, parsePosition)>
<cfoutput>
GetHttpTimeString = #date# <br />
SimpleDateFormat = #newDate# <br />
ParseDateTime = #ParseDateTime(date)#
</cfoutput>

When I run this code I get the following output:

GetHttpTimeString = Wed, 31 May 2006 14:33:52 GMT
SimpleDateFormat = {ts '2006-05-31 10:33:52'}
ParseDateTime = {ts '2006-05-31 14:33:52'}

Notice that the time is different in the result of the SimpleDateFormat. This is because it has converted the date to my local timezone. Also notice that the result of ParseDateTime has lost the timezone info.


Like this? Follow me ↯

Advanced Date Parsing with ColdFusion was first published on May 31, 2006.


FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

Comments

Pete, what can you do if you can't predict what format the date will be in? For instance, if you're parsing email dates you can't always predict the format of the sent date.
by Tom Mollerus on 05/31/2006 at 10:01:01 AM UTC
Tom, in that case you might still need to use regular expressions to detect the date pattern, but then you could still parse the date using this method.
by Pete Freitag on 05/31/2006 at 11:11:47 AM UTC
i've been a lot of this sort of thing for years for i18n stuff.
you don't need the ParsePosition bits, start w/DateFormat & do a getInstance (might want a DateTime instance). then you can do a simple parse(). you might also try just plain parse as i think it's inherited from DateFormat class.

as far as not knowing the date string format, you're out of luck if it's not one of the java standard ones (full-->short). which is why i *always* suggest using those instead of a custom mask. loop thru the standard styles in a cftry block tyrying to parse the date string. yeah i know, smashmouth programming but that's what most i18n java folks do.
by PaulH on 05/31/2006 at 11:50:46 AM UTC
let me nit pick a bit more, cf datetime's aren't exactly core java Dates, i think they're actually coldfusion.runtime.OleDateTime. see my blog entry on timezone hell: http://www.sustainablegis.com/blog/cfg11n/
by PaulH on 05/31/2006 at 7:12:06 PM UTC
Thanks for your comments as always Paul. While I'm sure they are not pure java.util.Date objects, they do inherit from that class I think - in which case you could say a coldfusion.runtime.OleDateTime "is a" java.util.Date

Just adding more nit for the picking :)
by Pete Freitag on 06/01/2006 at 3:01:32 PM UTC
well.....coldfusion.runtime.OleDateTime has different date units (decimal days) and epoch start (31-dec-1899) but yes you can manipulate it like a core java Date. i wonder if this is some DNA handed down from the cf's c++ days?
by PaulH on 06/02/2006 at 2:36:02 AM UTC
Thanks Pete! This has really helped me. I'm writing a "dashboard" for my company and I'm displaying the user's email. I couldn't figure out how to display the date correctly until I found this article.
by Robert on 11/12/2008 at 9:40:00 AM UTC