<?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>Jayway Team Blog &#187; calendar</title>
	<atom:link href="http://blog.jayway.com/tag/calendar/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tired of Date and Calendar?</title>
		<link>http://blog.jayway.com/2006/09/16/tired-of-date-and-calendar/</link>
		<comments>http://blog.jayway.com/2006/09/16/tired-of-date-and-calendar/#comments</comments>
		<pubDate>Sat, 16 Sep 2006 12:56:39 +0000</pubDate>
		<dc:creator>Ulrik Sandberg</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[User Experience]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[jodatime]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=250</guid>
		<description><![CDATA[It's not always easy to decide whether to add yet another dependency to yet another framework. It's especially hard when it's about a very central part of the JDK, like Date and Calendar. They are not great, there's little doubt about that. However, the benefits of a new framework would have to be numerous in [...]]]></description>
			<content:encoded><![CDATA[<p>It's not always easy to decide whether to add yet another dependency to yet another framework. It's especially hard when it's about a very central part of the JDK, like Date and Calendar. They are not great, there's little doubt about that. However, the benefits of a new framework would have to be numerous in order for us to make the switch. It must also be simple to convert between the standard classes and the framework classes. The impact on our project must be minimal. Ideally, it would be a question of dropping in a single jar-file. This article not only finds the weak spots of Date and Calendar, but also presents a compelling alternative.</p>
<h3>How bad are Date and Calendar?</h3>
<p>Consider <code>java.util.Date</code>. It has been around since JDK 1.0, and it's actually not a date, but a date and a time. In order to get the date (and the time), you call the <code>getTime()</code> method. It uses two-digit years from 1900, day-of-month is one-based, while month and hours are zero-based, it should have been immutable, and most methods have been deprecated in JDK 1.1. Since it's not final, it's possible to hack it by overriding critical methods, like for example <code>after</code>.</p>
<p>The state of <code>java.util.Calendar</code> is not much better. Month is zero-based, it should have been immutable, and it allows only one hour offset for daylight savings time. Some years have more, you know. British timezone in 1943, 1944 and 1945, for example, had two hours. The worst part is that it uses two different representations internally: a value for each field, and milliseconds since 1970. The scary part is that these two representations are not continuously kept in synch. Instead they are resynched as a side effect of other method calls, like <code>equals</code> for example. Wouldn't that be something? You call <code>equals</code> on your <code>Calendar</code> and suddenly it changes to a different date.</p>
<p>Then we have <code>java.util.SimpleDateFormat</code>. It requires a <code>Date</code> object, it's not very fast, and it's not thread-safe. If concurrent threads call its <code>format</code> method on a shared instance, it can produce bizarre results.</p>
<h4>They can't be that bad, can they?</h4>
<p>Let's print out a date. We want July 2, 1945 and the time should be 12.30 PM. Here we go:</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> date = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1945</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>date.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Sat Aug 02 12:30:00 CEST 3845</span>
&nbsp;</pre>
<p>Oops. Forgot the two-digit year and the zero-based month. Let's try again:</p>
<pre class="java">&nbsp;
<span style="color: #993333;">int</span> year = <span style="color: #cc66cc;">1945</span> - <span style="color: #cc66cc;">1900</span>;
<span style="color: #993333;">int</span> month = <span style="color: #cc66cc;">7</span> - <span style="color: #cc66cc;">1</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> date = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span>year, month, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>date.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Mon Jul 02 12:30:00 CEST 1945</span>
&nbsp;</pre>
<p>Yes, I know that the constructor used above is deprecated. This is how it really should be done:</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a> tz = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a>.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a> cal = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span>tz<span style="color: #66cc66;">&#41;</span>;
cal.<span style="color: #006600;">set</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1945</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">JULY</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">DateFormat</span></a> f= <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASimpleDateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yyyy-MM-dd hh:mm&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>f.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span>cal<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// java.lang.IllegalArgumentException: Cannot format given Object as a Date</span>
<span style="color: #808080; font-style: italic;">// at java.text.DateFormat.format(DateFormat.java:279)</span>
<span style="color: #808080; font-style: italic;">// at java.text.Format.format(Format.java:133)</span>
&nbsp;</pre>
<p>Oops. <code>DateFormat</code> cannot take a <code>Calendar</code>. Let's try with a <code>Date</code>:</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a> tz = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a>.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a> cal = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span>tz<span style="color: #66cc66;">&#41;</span>;
cal.<span style="color: #006600;">set</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1945</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">JULY</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">DateFormat</span></a> f= <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASimpleDateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yyyy-MM-dd hh:mm&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> date = cal.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>f.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span>date<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 1945-07-02 06:30</span>
&nbsp;</pre>
<p>Well, almost right. I asked for the time to be 12.30, not 6.30. Ah, of course. It's not enough to set the timezone on the <code>Calendar</code> that we used to get the <code>Date</code> that we want to print. The <code>DateFormat</code> that prints the <code>Date</code> also needs the timezone. Silly me.</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a> tz = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a>.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a> cal = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span>tz<span style="color: #66cc66;">&#41;</span>;
cal.<span style="color: #006600;">set</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1945</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Calendar</span></a>.<span style="color: #006600;">JULY</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">DateFormat</span></a> f= <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASimpleDateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yyyy-MM-dd hh:mm&quot;</span><span style="color: #66cc66;">&#41;</span>;
f.<span style="color: #006600;">setTimeZone</span><span style="color: #66cc66;">&#40;</span>cal.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> date = cal.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>f.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span>date<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 1945-07-02 12:30</span>
&nbsp;</pre>
<p>Finally.</p>
<h3>The Contender: Joda-Time</h3>
<p>There is a candidate that has all the characteristics we mentioned earlier, and more: <a href="http://joda-time.sourceforge.net/index.html">Joda-Time</a>. It's a single jar of 518k with no other dependencies. It replaces <code>Date</code>, <code>Calendar</code> and <code>TimeZone</code>. It has interfaces for datetimes, intervals and durations. The key classes are immutable. It has a pluggable calendar system where the default is ISO8601. And one more thing: it's easy to use.</p>
<h4>DateTime</h4>
<p>The most useful class in Joda-Time is the <code>DateTime</code>, which is immutable and thread-safe. It is measured in milliseconds since 1970, and it has simple getters for all fields. It supports timezones and multiple calendar systems. Its main purpose is to fill the most common needs as simple as possible:</p>
<pre class="java">&nbsp;
DateTime now = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333;">int</span> y = now.<span style="color: #006600;">getYear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> hour = now.<span style="color: #006600;">getHourOfDay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> sec = now.<span style="color: #006600;">getSecondOfMinute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333;">boolean</span> leap = now.<span style="color: #006600;">year</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">isLeap</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> daysInMonth = now.<span style="color: #006600;">dayOfMonth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getMaximumValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Since it's immutable, all mutating methods return new objects:</p>
<pre class="java">&nbsp;
DateTime now = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
DateTime yesterday = now.<span style="color: #006600;">minusDays</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">plusHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h4>Partial</h4>
<p>Partially defined datetimes have no timezone; they use local time. They are called "partial" because they don't have all the fields of a <code>DateTime</code>. We have <code>YearMonthDay</code>, <code>TimeOfDay</code> (hour, minute, second, and millisecond), and <code>Partial</code> (can store any fields). They can easily be converted to <code>DateTime</code>:</p>
<pre class="java">&nbsp;
YearMonthDay ymd = <span style="color: #000000; font-weight: bold;">new</span> YearMonthDay<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2005</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// September 20, 2005</span>
TimeOfDay tod = <span style="color: #000000; font-weight: bold;">new</span> TimeOfDay<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">14</span>, <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
DateTimeZone zone = DateTimeZone.<span style="color: #006600;">forID</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
DateTime dt = ymd.<span style="color: #006600;">toDateTimeAtCurrentTime</span><span style="color: #66cc66;">&#40;</span>zone<span style="color: #66cc66;">&#41;</span>;
DateTime dt2 = ymd.<span style="color: #006600;">toDateTime</span><span style="color: #66cc66;">&#40;</span>tod<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h4>Period</h4>
<p>A <code>Period</code> represents a period of time, like "5 years, 3 months, 2 days and 12 hours":</p>
<pre class="java">&nbsp;
Period period = <span style="color: #000000; font-weight: bold;">new</span> Period<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> years = period.<span style="color: #006600;">getYears</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> days = period.<span style="color: #006600;">getDays</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h4>Interval</h4>
<p>The class <code>Interval</code> represents an interval of time, represented by a start <code>DateTime</code> (included) and an end <code>DateTime</code> (excluded). Let's say we want to know how many complete weeks an interval is. To do that, we convert the <code>Interval</code> to a <code>Period</code> of the specific type we want. Using <code>PeriodType</code>, we can deduce that the interval below is 9 complete weeks (<code>P9W</code> is ISO8601-standard for a 9 week period):</p>
<pre class="java">&nbsp;
DateMidnight start = <span style="color: #000000; font-weight: bold;">new</span> DateMidnight<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2005</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// July 12, 2005</span>
DateMidnight end = <span style="color: #000000; font-weight: bold;">new</span> DateMidnight<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2005</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// September 15, 2005</span>
Interval interval = <span style="color: #000000; font-weight: bold;">new</span> Interval<span style="color: #66cc66;">&#40;</span>start, end<span style="color: #66cc66;">&#41;</span>;
Period period = interval.<span style="color: #006600;">toPeriod</span><span style="color: #66cc66;">&#40;</span>PeriodType.<span style="color: #006600;">weeks</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>period<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// P9W</span>
&nbsp;</pre>
<h4>Chronology</h4>
<p>Joda-Time features a pluggable calendar system based on the <code>Chronology</code> class. There are for example ISO9601, Gregorian, Julian, GJ, Buddhist, Ethiopic, and Coptic. The default is <code>ISOChronology</code>. A <code>Chronology</code> can be passed in as extra parameter whenever needed, as in the following example where we print the year 2006 as the Buddhists see it:</p>
<pre class="java">&nbsp;
Chronology buddhist = BuddhistChronology.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
DateTime now = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span>buddhist<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> year = now.<span style="color: #006600;">getYear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 2006</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>year<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 2549</span>
&nbsp;</pre>
<h4>Time Zone</h4>
<p>The <code>DateTimeZone</code> class encapsulates a time zone that contains not only the timezones in the current JDK implementations, but is also up to date with the most current time zone data available at the <a href="http://www.twinsun.com/tz/tz-link.htm">time zone database</a>.</p>
<pre class="java">&nbsp;
DateTimeZone.<span style="color: #006600;">UTC</span>;
DateTimeZone.<span style="color: #006600;">forID</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
DateTimeZone.<span style="color: #006600;">forOffsetHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
DateTimeZone.<span style="color: #006600;">forTimeZone</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a>.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Europe/Stockholm&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>One problem with the JDK <code>TimeZone</code> is that it returns timezone UTC (also known as GMT) if the full name cannot be understood:</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a> validTz = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a>.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>validTz<span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a> invalidTz = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATimeZone+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">TimeZone</span></a>.<span style="color: #006600;">getTimeZone</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/SomeUnknownCity&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>invalidTz<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// sun.util.calendar.ZoneInfo[id=&quot;Asia/Tokyo&quot;,offset=32400000,dstSavings=0,useDaylight=false,...</span>
<span style="color: #808080; font-style: italic;">// sun.util.calendar.ZoneInfo[id=&quot;GMT&quot;,offset=0,dstSavings=0,useDaylight=false,...</span>
&nbsp;</pre>
<p>By contrast, Joda-Time's <code>DateTimeZone</code> throws an <code>IllegalArgumentException</code> if it cannot understand the time zone name:</p>
<pre class="java">&nbsp;
DateTimeZone validTz = DateTimeZone.<span style="color: #006600;">forID</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/Tokyo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>validTz<span style="color: #66cc66;">&#41;</span>;
DateTimeZone invalidTz = DateTimeZone.<span style="color: #006600;">forID</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Asia/SomeUnknownCity&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>invalidTz<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Asia/Tokyo</span>
<span style="color: #808080; font-style: italic;">// java.lang.IllegalArgumentException: The datetime zone id is not recognised: Asia/SomeUnknownCity</span>
<span style="color: #808080; font-style: italic;">// at org.joda.time.DateTimeZone.forID(DateTimeZone.java:198)</span>
&nbsp;</pre>
<h4>Interoperability</h4>
<p>One of our requirements for a new framework was that it should be easy to convert to and from the standard classes. Constructors in Joda-Time take <code>Date</code>, <code>Calendar</code>, <code>String</code>, and <code>long</code>. From <code>DateTime</code> we can easily get JDK <code>Date</code> and <code>Calendar</code>. The <code>DateTimeZone</code> class can be constructed from a <code>TimeZone</code> and also return a <code>TimeZone</code>. In the example below, we easily convert from a standard <code>Date</code> to Joda-Time's <code>DateTime</code> to a <code>GregorianCalendar</code>, and from the <code>DateTime</code> back to a <code>Date</code>:</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> date = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">106</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">19</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Sep 19, 2006</span>
DateTime dt = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span>date<span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGregorianCalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">GregorianCalendar</span></a> c = dt.<span style="color: #006600;">toGregorianCalendar</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> d = dt.<span style="color: #006600;">toDate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h4>Formatting</h4>
<p>What about formatting then? Compared to <code>SimpleDateFormat</code>, the <code>DateTimeFormatter</code> is fast, flexible, thread-safe, and immutable. Formatting can be done in several ways: using a pattern, a style, or a format builder. You can either pass a formatter to the <code>toString</code> method for <code>DateTime</code>, or you can ask the formatter to print the <code>DateTime</code>:</p>
<pre class="java">&nbsp;
DateTimeFormatter f = DateTimeFormat.<span style="color: #006600;">longDateTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
DateTime dateTime = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>dateTime.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>f.<span style="color: #006600;">print</span><span style="color: #66cc66;">&#40;</span>dateTime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// May 25, 2006 9:45:09 PM CEST</span>
<span style="color: #808080; font-style: italic;">// May 25, 2006 9:45:09 PM CEST</span>
&nbsp;</pre>
<h4>DateTimeUtils</h4>
<p>If you need to stop time, or go back or forward in time, you can use the <code>DateTimeUtils</code> class. This code shows that it's possible to stop time:</p>
<pre class="java">&nbsp;
DateTime now = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
DateTimeUtils.<span style="color: #006600;">setCurrentMillisFixed</span><span style="color: #66cc66;">&#40;</span>now.<span style="color: #006600;">getMillis</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// time has stopped!</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a>.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 2006-05-25T23:21:37.562+02:00</span>
<span style="color: #808080; font-style: italic;">// 2006-05-25T23:21:37.562+02:00</span>
&nbsp;</pre>
<p>Turning back the clock is also possible:</p>
<pre class="java">&nbsp;
DateTime now = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
Period period = Period.<span style="color: #006600;">months</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>;
Duration dur = period.<span style="color: #006600;">toDurationFrom</span><span style="color: #66cc66;">&#40;</span>now<span style="color: #66cc66;">&#41;</span>;
DateTimeUtils.<span style="color: #006600;">setCurrentMillisOffset</span><span style="color: #66cc66;">&#40;</span>dur.<span style="color: #006600;">getMillis</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// we've gone back in time!</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 2006-04-25T23:24:03.796+02:00</span>
&nbsp;</pre>
<p>Don't forget to reset to system time:</p>
<pre class="java">&nbsp;
DateTimeUtils.<span style="color: #006600;">setCurrentMillisSystem</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// we're back to normal</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 2006-05-25T23:29:41.281+02:00</span>
&nbsp;</pre>
<h3>Where were we?</h3>
<p>Oh, yes. Printing dates. Let's print out the same date using Joda-Time. It was 12.30 PM on July 2, 1945:</p>
<pre class="java">&nbsp;
DateTime date = <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1945</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
DateTimeFormatter formatter = DateTimeFormat.<span style="color: #006600;">forPattern</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yyyy-MM-dd HH:mm&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><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>date.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span>formatter<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// 1945-07-02 12:30</span>
&nbsp;</pre>
<p>Couldn't be simpler.</p>
<h3>Conclusion</h3>
<p>The existing classes for date and time have drawbacks, some of them serious. The contender is Joda-Time, which is a complete rewrite and represents a mature API with a strong focus on usability, interoperability, stability and performance. What are you waiting for? Now is the time.</p>
<p>Update: <a href="http://tech.puredanger.com/java7/#jsr310">It seems</a> Java7 will be using JSR310, which basically is Joda-Time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2006/09/16/tired-of-date-and-calendar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

