<p>In java, when using SimpleDateFormat with the pattern:</p>

<pre><code>yyyy-MM-dd'T'HH:mm:ss.SSSZ
</code></pre>

<p>the date is outputted as:</p>

<pre><code>"2002-02-01T18:18:42.703-0700"
</code></pre>

<p>In xquery, when using the xs:dateTime function, it gives the error:</p>

<pre><code>"Invalid lexical value [err:FORG0001]"
</code></pre>

<p>with the above date. In order for xquery to parse properly, the date needs to look like:</p>

<pre><code>"2002-02-01T18:18:42.703-07:00" - node the ':' 3rd position from end of string
</code></pre>

<p>which is based on the ISO 8601, whereas Java date is based on the RFC 822 standard.</p>

<p>I would like to be able to easily specify the timezone in Java so that it will output the way that xquery wants.</p>

<p>Thanks!</p>

Try this:
```
```
static public String formatISO8601(Calendar cal) {
MessageFormat format = new MessageFormat("{0,time}{1,number,+00;-00}:{2,number,00}");

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(cal.getTimeZone());
format.setFormat(0, df);

long zoneOff = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET) / 60000L;
int zoneHrs = (int) (zoneOff / 60L);
int zoneMins = (int) (zoneOff % 60L);
if (zoneMins < 0)
    zoneMins = -zoneMins;

return (format.format(new Object[] { cal.getTime(), new Integer(zoneHrs), new Integer(zoneMins) }));
}
```
```