In the UK, I'd like to get the current offset from UTC/GMT. Currently the offset is 1 hour, but there seems to be no way of finding this.
Code
TimeZone timeZone = TimeZone.getDefault();
logger.debug("Timezone ID is '" + timeZone.getID() + "'");
if (timeZone.getID().equals("GMT")) {
timeZone = TimeZone.getTimeZone("Europe/London");
logger.debug("New timezone is '" + timeZone.getID() + "'");
}
Long eventDateMillis = Long.parseLong(eventDateValue.getKeyValue());
int timezoneOffset = timeZone.getOffset(eventDateMillis);
logger.debug("Offset for " + eventDateValue + "(" + eventDateMillis + ") using timezone " + timeZone.getDisplayName() + " is " + timezoneOffset);
returns debug output
Wed 2012/09/19 16:38:19.503|Debug|DataManagement|Timezone ID is 'GMT'
Wed 2012/09/19 16:38:19.503|Debug|DataManagement|New timezone is 'GMT'
Wed 2012/09/19 16:38:19.557|Debug|DataManagement|Offset for 18 Sep 2012 09:00(1347958800000) using timezone Greenwich Mean Time is 0
In other words, timezone 'GMT' is returning an offset of zero and I can't find how to set it to something that does return the correct offset.
If someone can provide a working code sample for JodaTime, that would do but I'd really like to avoid having to install a separate library just for what should be a one-liner.
The Java version is 7.
In other words, timezone 'GMT' is returning an offset of zero and I can't find how to set it to something that does return the correct offset.
0 is the correct offset for GMT, permanently.
You want "Europe/London", which is the time zone which switches between GMT and BST.
EDIT: I hadn't originally noticed that you're trying to get Europe/London. It looks like the time zone database your JVM is using is basically messed up. You could either try fixing that, or simply use Joda Time which comes with its own copy of tzdb. Sample code:
DateTimeZone zone = DateTimeZone.forID("Europe/London");
long offset = zone.getOffset(new Instant());
System.out.println(offset); // 3600000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With