I am trying the get the time zone offset of the browser/client for a specific date in the future. I know you can get the current time zone offset using the following :
But is it possible to get the time zone offset for a date in the future ? The reason I need this information is that I am trying to make sure that a specific time on any day will always be displayed as the same time no matter if you are in Daylight Saving Time or not.
Unless I’m missing something, here’s the best that you can do based on the current available information
int browserRawOffset = getBrowser().getRawTimezoneOffset();
String[] tzs = TimeZone.getAvailableIDs(browserRawOffset);
for (String tz : tzs) {
boolean inDaylightTime = TimeZone.getTimeZone(tz).inDaylightTime(dateIWantToCheck);
}
so you’ll have a list of potential TimeZones the browser could be in, based on the raw offset, but if there’s more than one, could be that some of them are in DST and others are not.
You might get better results with a JavaScript snippet, perhaps using moment.js, but I’m not sure if all browsers are capable of giving accurate TZ information (more than what you get with RawTimezoneOffset).
Thank you, I managed to get something that works well. Javascript gives you the offset the opposite way that java does thought so you got to be careful about that. getBrowserTimeZoneOffset() gives me -240 while javascript date.getTimezoneOffset() gives me 240 for today in Eastern Standard Time.
final int currentOffset = TimeZoneUtils.getBrowserTimeZoneOffset();
JavaScript.getCurrent().addFunction("fixTimeZoneOffsetForDate", new JavaScriptFunction() {
@Override
public void call(JsonArray arguments) {
int specificDateOffset = -1 * (int) arguments.getNumber(0);
Calendar cal = Calendar.getInstance();
cal.setTime(value);
cal.add(Calendar.MINUTE, specificDateOffset - currentOffset);
nextDateControl.setReadOnly(false);
nextDateControl.setValue(cal.getTime());
nextDateControl.setReadOnly(true);
}
});
JavaScript.getCurrent().execute(
"var date = new Date(" + value.getTime()
+ "); fixTimeZoneOffsetForDate(date.getTimezoneOffset());");