Advanced Date Parsing with 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.
add to del.icio.us
| Tags: date, time, utc, gmt, rfc822, parsedatetime, simpledateformat, gethttptimestring, cfml
Trackbacks
Trackback Address: 569/4A9F4F5433D824EB95D3D5E663E35B24
Comments
On 05/31/2006 at 10:01:01 AM EDT Tom Mollerus wrote:
1
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.
On 05/31/2006 at 11:11:47 AM EDT Pete Freitag wrote:
2
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.
On 05/31/2006 at 11:50:46 AM EDT PaulH wrote:
3
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.
On 05/31/2006 at 7:12:06 PM EDT PaulH wrote:
4
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/
On 06/01/2006 at 3:01:32 PM EDT Pete Freitag wrote:
5
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 :)
On 06/02/2006 at 2:36:02 AM EDT PaulH wrote:
6
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?
On 10/12/2006 at 10:34:50 AM EDT Anonymous wrote:
7
how to get the milliseconds from Jan 1,1970 00:00:00 GMT ?
On 11/12/2008 at 9:40:00 AM EST Robert wrote:
8
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.
Post a Comment
Recent Entries
- Cache Template in Request Setting Explained
- What Version of Java is ColdFusion Using?
- ColdFusion 9 Performance Brief from Adobe
- Request Filtering in IIS 7 Howto
- J2EE Session Cookies on ColdFusion / JRun
- Hands on ColdFusion Security Training
- ColdFusion 9 Solr Vulnerability - Are you at Risk?
- FCKEditor Year 2010 Bug for Firefox 3.6 with ColdFusion
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.
Just adding more nit for the picking :)







