Sergio Arrighi Sergio Arrighi - 2 years ago 170
Java Question

Joda parse ISO8601 date in GMT timezone

I have a ISO 8601 date, lets say:


My machine timezone is

I'm trying to use joda to parse this and convert it to the respective GMT date and time:

DateTimeFormatter simpleDateISOFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mmZZ");
creationDate = simpleDateISOFormat.withZone(DateTimeZone.UTC)
.parseDateTime(date + "T" + time)

Now the result that I'm expecting is
Fri Jan 20 00:00:00 CET 2012

Instead I'm getting:
Fri Jan 20 01:00:00 CET 2012

I believe this is because I'm in timezone
GMT + 1

Is there a way to parse the date faking to be in a different time zone?

Edit: Basically the problem is when I call the
method. The method converts the
into a
as I need to do but I transforms it in local time.

Do someone know a conversion method which doesn't impose this limitation?

Answer Source

Here's a working groovy testcase. Shows how times in other timezones can be displayed.

import org.joda.time.*
import org.joda.time.format.*

    @Grab(group='joda-time', module='joda-time', version='1.6.2')

class JodaTimeTest extends GroovyTestCase {

    void testTimeZone() {
        DateTimeFormatter parser    = ISODateTimeFormat.dateTimeParser()
        DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis()

        DateTime dateTimeHere     = parser.parseDateTime("2012-01-19T19:00:00-05:00")

        DateTime dateTimeInLondon = dateTimeHere.withZone(DateTimeZone.forID("Europe/London"))
        DateTime dateTimeInParis  = dateTimeHere.withZone(DateTimeZone.forID("Europe/Paris"))

        assertEquals("2012-01-20T00:00:00Z", formatter.print(dateTimeHere))
        assertEquals("2012-01-20T00:00:00Z", formatter.print(dateTimeInLondon))
        assertEquals("2012-01-20T01:00:00+01:00", formatter.print(dateTimeInParis))


  • You'll have to adjust the assertions, because I'm located in the London timezone :-)
  • The "withZone" method changes the DateTime object's metadata to indicate it's timezone. Still the same point in time, just displayed with a different offset.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download