<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Java Date Validation is Annoying (Regexes too)</title>
	<atom:link href="http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Wed, 01 Sep 2010 13:06:51 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Matt Benson</title>
		<link>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/comment-page-1/#comment-4051</link>
		<dc:creator>Matt Benson</dc:creator>
		<pubDate>Mon, 18 Jun 2007 13:00:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/#comment-4051</guid>
		<description>wrt your regexp example:

//given String input, String regex:

input.matches(regex) is a convenience method for:
Pattern.matches(regex, input)

which in turn is short for:
Pattern.compile(regex).matcher(input).matches()

If you&#039;re looking for an occurrence as opposed to matching against the entire regex, use:
Pattern.compile(regex).matcher(input).find()

And of course there&#039;s no reason not to cache the compiled Pattern in a real-world application.</description>
		<content:encoded><![CDATA[<p>wrt your regexp example:</p>
<p>//given String input, String regex:</p>
<p>input.matches(regex) is a convenience method for:<br />
Pattern.matches(regex, input)</p>
<p>which in turn is short for:<br />
Pattern.compile(regex).matcher(input).matches()</p>
<p>If you&#8217;re looking for an occurrence as opposed to matching against the entire regex, use:<br />
Pattern.compile(regex).matcher(input).find()</p>
<p>And of course there&#8217;s no reason not to cache the compiled Pattern in a real-world application.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: P.Hill</title>
		<link>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/comment-page-1/#comment-3979</link>
		<dc:creator>P.Hill</dc:creator>
		<pubDate>Fri, 15 Jun 2007 23:28:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/#comment-3979</guid>
		<description>Why does it work? &#039;cause SimpleDateFormat has the ability to deal with Medieval dates!
Thus you need to explicitly check for that.

[java]
	GregorianCalendar cal = new GregorianCalendar();

	try {
		Date date = sdf.parse(str);
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		if (year 

[/java]</description>
		<content:encoded><![CDATA[<p>Why does it work? 'cause SimpleDateFormat has the ability to deal with Medieval dates!<br />
Thus you need to explicitly check for that.</p>
<div class="syntax_hilite">
<div id="java-1">
<div class="java"><a href="http://www.google.com/search?q=allinurl%3AGregorianCalendar+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">GregorianCalendar</span></a> cal = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AGregorianCalendar+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">GregorianCalendar</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <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> date = sdf.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cal.<span style="color: #006600;">setTime</span><span style="color: #66cc66;">&#40;</span>date<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> year = cal.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ACalendar+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">YEAR</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>year</div>
</div>
</div>
<p></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jason</title>
		<link>http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/comment-page-1/#comment-3846</link>
		<dc:creator>jason</dc:creator>
		<pubDate>Thu, 07 Jun 2007 19:44:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/06/07/java-date-validation-is-annoying-regexes-too/#comment-3846</guid>
		<description>Or you could just use SimpleDateFormat(&quot;MM-dd-yy&quot;) to parse for both 2 and 4 digit years.

Example.  This will always work in the first block.

[java]
	public static void main(String[] args) {

		String dateIn = &quot;01-01-3020&quot;;

		SimpleDateFormat sdf1 = new SimpleDateFormat(&quot;MM-dd-yy&quot;);
		SimpleDateFormat sdf2 = new SimpleDateFormat(&quot;MM-dd-yyyy&quot;);

		Date myDate = null;

		try {
			myDate = sdf1.parse(dateIn);
			System.out.println(&quot;ONE&quot;);
		} catch (ParseException e) {
			try {
				myDate = sdf2.parse(dateIn);
				System.out.println(&quot;TWO&quot;);
			} catch (ParseException e1) {
				System.out.println(&quot;Date Format Error&quot;);
			}
		}

		System.out.println(myDate);
	}
[/java]</description>
		<content:encoded><![CDATA[<p>Or you could just use SimpleDateFormat("MM-dd-yy") to parse for both 2 and 4 digit years.</p>
<p>Example.  This will always work in the first block.</p>
<div class="syntax_hilite">
<div id="java-2">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<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><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &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> dateIn = <span style="color: #ff0000;">"01-01-3020"</span>;</p>
<p>&nbsp; &nbsp; &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> sdf1 = <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-yy"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &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> sdf2 = <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>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <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> myDate = <span style="color: #000000; font-weight: bold;">null</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDate = sdf1.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>dateIn<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"ONE"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <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> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDate = sdf2.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>dateIn<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"TWO"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <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> e1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Date Format Error"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>myDate<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
]]></content:encoded>
	</item>
</channel>
</rss>
