<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>We Heart Code &#187; rant</title>
	<atom:link href="http://www.weheartcode.com/category/rant/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Sun, 17 Jan 2010 02:16:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Date Validation is Annoying (Regexes too)</title>
		<link>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/</link>
		<comments>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 14:47:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/</guid>
		<description><![CDATA[*EDIT* Thanks for the tips in the comments.
Today I had a user complain that they could enter 2 digit years in their app. Hmm I took a look at the code and I didn't see how that was possible. Here was my date validation method, more after the jump.



public static boolean isDate&#40;String str&#41;
&#160; &#123;
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>*EDIT* Thanks for the tips in the comments.</p>
<p>Today I had a user complain that they could enter 2 digit years in their app. Hmm I took a look at the code and I didn't see how that was possible. Here was my date validation method, more after the jump.<br />
<span id="more-21"></span></p>
<div class="syntax_hilite">
<div id="java-4">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">boolean</span> isDate<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> str<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>str == <span style="color: #000000; font-weight: bold;">null</span> || str.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">""</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASimpleDateFormat+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a> sdf = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ASimpleDateFormat+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"MM-dd-yyyy"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; sdf.<span style="color: #006600;">setLenient</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// force pattern to be enforced</span><br />
&nbsp; &nbsp; java.<span style="color: #006600;">sql</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> sqlDate;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; sqlDate = <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006600;">sql</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span>sdf.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AParseException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ParseException</span></a> e<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Hmm looks OK to me, the problem was that SimpleDateFormat doesn't work like I thought it would, turns out 01-01-20 will pass through the MM-dd-yyyy mask! So how do you enforce century matching with SimpleDateFormat?  Who knows, I was too pissed off reading through the javadoc.  Rather than waste time figuring it out I thought I'd just  use a regular expression to make sure the string ends with 4 digits.</p>
<div class="syntax_hilite">
<div id="java-5">
<div class="java"><span style="color: #808080; font-style: italic;">//WRONG DUMMY!</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> p = <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>d<span style="color: #000099; font-weight: bold;">\\</span>d<span style="color: #000099; font-weight: bold;">\\</span>d<span style="color: #000099; font-weight: bold;">\\</span>d$"</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!str.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Wait that didn't work either. Now I'm really getting steamed, turns out not only do I not understand the SimpleDateFormat class, I also don't understand the matches method, turns out it doesn't do <em>partial</em> pattern matching. </p>
<p>So the correct pattern needs to match everything before the last 4 digits as well. So here's my final method.</p>
<div class="syntax_hilite">
<div id="java-6">
<div class="java"><span style="color: #808080; font-style: italic;">//this works</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">boolean</span> isDate<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> str<span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>str == <span style="color: #000000; font-weight: bold;">null</span> || str.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">""</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> p = <span style="color: #ff0000;">".*<span style="color: #000099; font-weight: bold;">\\</span>d<span style="color: #000099; font-weight: bold;">\\</span>d<span style="color: #000099; font-weight: bold;">\\</span>d<span style="color: #000099; font-weight: bold;">\\</span>d$"</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!str.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASimpleDateFormat+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a> sdf = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ASimpleDateFormat+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"MM-dd-yyyy"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; sdf.<span style="color: #006600;">setLenient</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// force pattern to be enforced</span><br />
&nbsp; &nbsp; java.<span style="color: #006600;">sql</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> sqlDate;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; sqlDate = <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006600;">sql</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span>sdf.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AParseException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ParseException</span></a> e<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Hooray, regex checks for a 4 digit year then simple date format does the leap year legwork and other shenanigans. </p>
<p>If someone who understands how the hell SimpleDateFormat works wants to enlighten me, by all means do so. This just goes to show you, don't make assumptions in Java, if you're not creating 74 objects and catching 812 exceptions then you're probably doing it wrong!</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/&amp;title=Java+Date+Validation+is+Annoying+%28Regexes+too%29" title="Add 'Java Date Validation is Annoying (Regexes too)' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Java Date Validation is Annoying (Regexes too)' to Del.icio.us" alt="Add 'Java Date Validation is Annoying (Regexes too)' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/&amp;title=Java+Date+Validation+is+Annoying+%28Regexes+too%29" title="Add 'Java Date Validation is Annoying (Regexes too)' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Java Date Validation is Annoying (Regexes too)' to digg" alt="Add 'Java Date Validation is Annoying (Regexes too)' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Java+Date+Validation+is+Annoying+%28Regexes+too%29&amp;u=http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/" title="Add 'Java Date Validation is Annoying (Regexes too)' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Java Date Validation is Annoying (Regexes too)' to FURL" alt="Add 'Java Date Validation is Annoying (Regexes too)' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/&amp;title=Java+Date+Validation+is+Annoying+%28Regexes+too%29" title="Add 'Java Date Validation is Annoying (Regexes too)' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Java Date Validation is Annoying (Regexes too)' to reddit" alt="Add 'Java Date Validation is Annoying (Regexes too)' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/" title="Add 'Java Date Validation is Annoying (Regexes too)' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Java Date Validation is Annoying (Regexes too)' to Technorati" alt="Add 'Java Date Validation is Annoying (Regexes too)' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/&amp;title=Java+Date+Validation+is+Annoying+%28Regexes+too%29" title="Add 'Java Date Validation is Annoying (Regexes too)' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Java Date Validation is Annoying (Regexes too)' to Google Bookmarks" alt="Add 'Java Date Validation is Annoying (Regexes too)' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
