Perry Smith

Java Questions & Answers

Home / Java Q & A Archive Page 1 / Java Q & A Archive / Java Q & A Main Page

8-15-98

Hi
I am trying to include a clock on my site that will always tell the time in central Europe (i.e. GMT + 2 hours). But I haven't found a way of referring directly to the time zones (CET). Getting it to show local time is ok but making adjustments for other time zones seems to give different results depending on which browser you use and which operating system (Explorer is 7 hours different to Navigator - whereas local time is correct on both).

Any suggestions?

john
ibebologna@delphi.com


Responses
8-20-98

I really don't know much JavaScript... but if this were Java and I needed to create an applet that would display a time in some other time zone ... this is how I would implement it (it's a lot more complex than I thought) ... I don't know if the TimeZone class exists in JavaScript; if not, you can stop reading now b/c it's all based on that class.

/* I imported these classes:
import java.util.TimeZone;
import java.util.Date;
*/
System.out.println(TimeZone
.getDefault().getID()); //remove
TimeZone tzLocal = TimeZone.getDefault();
int iOffset = tzLocal.getRawOffset();
System.out.println("UTC offset = "
+ iOffset);//remove
int iOffsetHr = iOffset
/ (1000 * 60 * 60); //remove
System.out.println("UTC offset in hr,s: "
+ iOffsetHr); //remove
boolean isTZInDaylightSavings =
tzLocal.inDaylightTime(new Date());
if (isTZInDaylightSavings)
{
iOffset = iOffset - (1000*60*60);
}
System.out.println("In Daylight Savings? "
+ isTZInDaylightSavings); //remove


TimeZone tzNew = TimeZone.getTimeZone("ECT");
int iNewOffset = tzNew.getRawOffset();
System.out.println("New UTC offset = "
+ iNewOffset); //remove
int iNewOffsetHr = iNewOffset
/ (1000 * 60 * 60); //remove
System.out.println("New UTC offset in hr,s: "
+ iNewOffsetHr); //remove
boolean isNewTZInDaylightSavings =
tzNew.inDaylightTime(new Date());
if (isNewTZInDaylightSavings)
{
iNewOffset = iNewOffset - (1000*60*60);
}
System.out.println("New In Daylight Savings?"
+ isNewTZInDaylightSavings); //remove


int iOffsetDifference = iNewOffset - iOffset;
Date date1 = new Date();
date1.setTime((new Date()).getTime()
+ iOffsetDifference);
System.out.println("Time in " + tzNew.getID()
+ " is " + date1.toString()); //remove

You'll notice there are lines that say remove ... these are lines I used for debugging purposes. I left them since they might help you as well.

I hope this helps ... I know I learned a little.

Mike


add'l note:

This final date ("date1") is almost correct for displaying the time in "ECT", except for the minor fact that since it was created on the local machine, it has the time zone label for the time zone of the local machine, even though it actually contains the time for ECT ... So, say it were 3pm here (and the central europe time is 6 hr.s ahead) it might say something like "Thu Aug 20 21:00:00 EDT 1998" if a date1.toString() were done ... you see that everything is correct to display info about the time in ECT except that "EDT" label in there

Mike


Home / Java Q & A Archive Page 1 / Java Q & A Archive / Java Q & A Main Page