SimpleDateFormat timezone bug on Android


<p>I've been trying to isolate a bug in my application. I succeeded in producing the following "riddle":</p>

<pre class="lang-java prettyprint-override"><code>SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date d = f1.parse("2012-01-01T00:00:00+0700");
String s1 = f1.format(d); // 2011-12-31T18:00:00+0700
String s2 = f2.format(d); // 2011-12-31T18:00:00+0100
</code></pre>

<p>I get the values in comments when I run this code <strong>on Android API 7</strong> (yes, really). This behavior depends on particular Java implementation.</p>

<p>My questions are:</p>

<ul>
<li>Why s1 does not equal s2?</li>
<li>And more importantly, <strong>why s1 is incorrect?</strong> While <code>s2</code> points to a proper point in time, <code>s1</code> does not. There seems to be a bug in Android's SimpleDateFormat implementation.</li>
</ul>

<p><strong>ANSWER TO QUESTION 1:</strong> See the answer by BalusC:</p>

<ul>
<li><em>[After using <code>SimpleDateFormat#parse</code>] any TimeZone value that has previously been set by a call to setTimeZone may need to be restored for further operations.</em></li>
</ul>

<p><strong>ANSWER TO QUESTION 2:</strong> See the answer by wrygiel (myself).</p>

<ul>
<li><em>This is due to a bug in Android 2.1 (API 7).</em></li>
</ul>
