Convert UTC to LocalDateTime in Joda?

        DateTime dt = new DateTime("2014-09-15T21:20:14");
        System.out.println(dt);
        System.out.println(dt.plusMillis(581042272).toDateTime().toLocalDateTime().toDateTime(DateTimeZone.forID("GMT")));

the time in dt is in UTC, I want to set the time in dt plus milliseconds to GMT? However, the time is still printed as UTC (1 hour behind GMT). How can I set it so it's one hour in front?

2014-09-15T21:20:14.000+01:00
2014-09-22T14:44:16.272Z

I know the time is exactly one hour behind because I made this request at 15:44:16 GMT

Jon Skeet
people
quotationmark

Your DateTime is actually not in UTC - it's in the system default time zone. To fix it, you just need to tell it that the value you're passing in is in UTC:

DateTime dt = new DateTime("2014-09-15T21:20:14", DateTimeZone.UTC);
System.out.println(dt);
DateTime other = dt.plusMillis(581042272);
System.out.println(other);

Output:

2014-09-15T21:20:14.000Z
2014-09-22T14:44:16.272Z

Also note that you can't have made the request at 15:44:16 GMT, as that hasn't occurred yet. At the time I'm writing this, it's 16:05 British Summer Time, therefore 15:05 GMT. It's important to understand that the time zone in the UK isn't "GMT" - that's just the part of the time zone when we're not observing daylight savings.

If you want to convert to the UK time zone, you want:

DateTime other = dt.plusMillis(581042272)
    .withZone(DateTimeZone.forID("Europe/London"));

people

See more on this question at Stackoverflow