<?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>Christian Schenk&#187; date</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/date/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Friday the 13th with Joda Time</title>
		<link>http://www.christianschenk.org/blog/friday-13th-joda-time/</link>
		<comments>http://www.christianschenk.org/blog/friday-13th-joda-time/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 07:15:52 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[13th]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[friday]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[joda time]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=114</guid>
		<description><![CDATA[I&#8217;ve always wanted to try Joda Time and in this post I&#8217;d like to present some code that finds Friday the 13th dates. Wouldn&#8217;t it be interesting to know when the next Friday the 13th is or which year has got the most occurrences of this particular day?
The Eclipse project with the code for this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always wanted to try <a href="http://joda-time.sourceforge.net/">Joda Time</a> and in this post I&#8217;d like to present some code that finds <a href="http://en.wikipedia.org/wiki/Friday_the_13th">Friday the 13th</a> dates. Wouldn&#8217;t it be interesting to know when the next <em>Friday the 13th</em> is or which year has got the most occurrences of this particular day?</p>
<p>The Eclipse project with the code for this post can be downloaded as <a href="http://data.christianschenk.org/friday-13th-joda-time/JodaFriday13th.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/friday-13th-joda-time/JodaFriday13th.zip">zip</a>. You can browse the code online <a href="http://data.christianschenk.org/friday-13th-joda-time/xref/">here</a>.</p>
<p><span id="more-114"></span></p>
<h2>The code</h2>
<p>Using Joda Time it&#8217;s super easy to iterate over the next days using the <code>plusDays</code> method of <code>DateTime</code>. Finding Friday the 13th dates is straight forward this way. Sure, we could optimize it but Joda Time seems to be really fast so there&#8217;s no need to do this.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> List<span style="color: #339933;">&lt;</span>DateTime<span style="color: #339933;">&gt;</span> getNextNthFriday13th<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>DateTime<span style="color: #339933;">&gt;</span> fridays <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>DateTime<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  DateTime dt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">int</span> curN <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>curN <span style="color: #339933;">&lt;</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dt <span style="color: #339933;">=</span> dt.<span style="color: #006633;">plusDays</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dt.<span style="color: #006633;">getDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dt.<span style="color: #006633;">getDayOfWeek</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> DateTimeConstants.<span style="color: #006633;">FRIDAY</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
&nbsp;
    fridays.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span>dt.<span style="color: #006633;">toInstant</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    curN<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">return</span> fridays<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finding the next year with the most Friday the 13th dates is simple too. Just get the next <em>n</em> occurrences of Friday the 13th and sort them by year to find the one with the highest amount. Another way to solve this is this: just search the next year that has got three Friday the 13th dates and you&#8217;re done. Have a look at the code of <a href="http://data.christianschenk.org/friday-13th-joda-time/xref/org/christianschenk/friday13th/Friday13th.html#62">this method</a> to see my quick solution.</p>
<h2>Conclusion</h2>
<p>It&#8217;s a real joy to use Joda Time because its API has got methods for a lot of use cases. Although I haven&#8217;t done any performance tests yet it seems to be lightning fast. I recommend that you should definitely have a look at it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/friday-13th-joda-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
