I thought 2011-10-23 12:00:00 would remain the same as UTC and that the Converted date would be 2011-10-23 17:00:00.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = formatter.parse("2011-10-23 12:00:00");
LocalDateTime ldt = new DateTime(dt).withZone(DateTimeZone.UTC).toLocalDateTime();
LOGGER.warn("Original date: " + ldt.toDateTime().toDate().toString());
DateTime cvtldt = ldt.toDateTime(DateTimeZone.forID("-05:00"));
LOGGER.warn("Converted date: " + cvtldt.toLocalDateTime().toDateTime().toDate().toString());
I don’t understand why the output is minus one hour?
Original date: Sun Oct 23 11:00:00 BST 2011
Converted date: Sun Oct 23 11:00:00 BST 2011
You’re using
Date.toString()which always uses the local time zone. See how your string contains “BST”?Ideally, stick to just Joda Time for as much of the time as you can:
Dateunless you need toDate.toString()if you can possibly avoid it; you have no control over its format.It’s not clear what you’re really trying to achieve, but you almost certainly don’t want to do this many conversions. For example, you’re calling
toLocalDateTime()followed bytoDateTime()again – which means it’s using the system default time zone, after you’d carefully specified UTC in the previous conversion…Your code contains the following conversions (in this order):
What do you think the chances of all those conversions being both necessary and correctly specified are? 😉