[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Timezone.getOffset() not returning right value

Posted on 2007-10-19
23
Medium Priority
?
1,509 Views
Last Modified: 2012-06-21
I am using JDK 1.6.  I am in the Eastern Time Zone in the US.  Right now we are four hours behind GMT.  Why does the following code return a five hour offset instead of a four hour offset?

TimeZone tz = TimeZone.getTimeZone("EST");
Calendar calLocal = Calendar.getInstance(tz);
return tz.getOffset(calLocal.get(Calendar.ERA), calLocal.get(Calendar.YEAR), calLocal.get(Calendar.MONTH), calLocal.get(Calendar.DAY_OF_MONTH), calLocal.get(Calendar.DAY_OF_WEEK), calLocal.get(Calendar.MILLISECOND)));
0
Comment
Question by:david_m_jacobson
  • 8
  • 6
  • 4
  • +2
22 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 20109285
EST = GMT-5

According to the Time Zone tables from Microsoft Windows XP.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20109290
You can also read this under your post!!!!!!!

;JOOP!
0
 

Author Comment

by:david_m_jacobson
ID: 20109400
This is from the Javadocs on TimeZone:

Gets the time zone offset, for current date, modified in case of daylight savings. This is the offset to add to UTC to get local time.
This method returns a historically correct offset if an underlying TimeZone implementation subclass supports historical Daylight Saving Time schedule and GMT offset changes.


This seems to indicate that it handles figuring out whether there is daylight savings.  Right now it's daylight savings so even though the timezone indicates GMT -5:00, right now because of daylight savings, it's GMT -4:00.

What's the deal?  What am I not understanding?  Why doesn't TimeZone figure out that it's daylight savings like it seems to indicate in the Javadocs for TimeZone?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 24

Expert Comment

by:sciuriware
ID: 20109807
I suggest you add in your program to:
TimeZone tz = TimeZone.getTimeZone("EST");
the following:

boolean dst = tz.inDaylightTime(new Date());

If the result is true, then your timezone is probably not set correctly.
If the result is false, daylight savings time is not in effect on your machine.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20109833
And for the offset I suggest:

long offset = tz.getOffset(new Date().getTime()) ; // A bit simpler, isn't it?

;JOOP!
0
 

Author Comment

by:david_m_jacobson
ID: 20110067
This doesn't work for me.  FIrst, I need to handle any date, not just the date on my computer right now.  Second, now that I know whether it's daylight savings, what do I do?  Add an hour?  Subtract an hour?  Why does the Javadocs of getOffset() indicate that it handles daylight savings when it doesn't seem to?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20110283
1) if this works correct for NOW, it will always work,
2) I asked you to check if daylight savings time is enabled on your computer.
----- did you know that it can be disabled?

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20110744
I think you need to set your timezone thus:

TimeZone tz = TimeZone.getTimeZone("EST5EDT");
0
 

Author Comment

by:david_m_jacobson
ID: 20110867
The problem is that "EST" is an example.  The actual value of the time zone can be any value from around the world.  How do I handle every time zone???  Shouldn't this be handled by the Java JDK?  Can someone give me a list of time zone strings from around the world that take into consideration daylight savings for the appropriate zones?

HST
AST
PST
PNT
MST
CST
IET
EST
PRT
CNT
AGT
GMT
ECT
CAT
ART
EAT
MET
MIT
CNT
WET
IRAN
NET
PLT
IST
BST
VST
CTT
JST
ACT
AET
SST
NST
0
 

Author Comment

by:david_m_jacobson
ID: 20110875
The above list is my current list of timezone strings.  Is this the correct list that will handle daylight savings appropriately?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20111017
The default timezone is taken from the system
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20111203
david_m_jacobson, if you are on Windows, double click the time right on the system tray:
-> Date And Time Properties.
Then click the tab Time Zone and open the dropdown to see all time zones with their offsets.

If a time zone has daylight savings time in inbuilt, but if it is enabled is an Administrator task
per machine!!!

Did you now ever check for daylight savings time enabled or not?

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20111213
P.S.: daylight savings time enabled/disabled is shown in the dialog described above.

;JOOP!
0
 

Author Comment

by:david_m_jacobson
ID: 20111236
I don't care whether daylight savings is enabled or disabled on my computer.  I was using EST as an example.  The timezone can be any timezone from around the world.  How do I make sure that I get the right offset for any timezone?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20111257
You got the right offset already, but you said it was not adjusted for daylight saving.

;JOOP!
0
 
LVL 28

Expert Comment

by:rrz
ID: 20111670
>Why does the following code return a five hour offset instead of a four hour offset?
I ran your original code. I get  -4    when I use
return tz.getOffset(calLocal.get(Calendar.ERA), calLocal.get(Calendar.YEAR), calLocal.get(Calendar.MONTH), calLocal.get(Calendar.DAY_OF_MONTH), calLocal.get(Calendar.DAY_OF_WEEK), calLocal.get(Calendar.MILLISECOND)) / 3600000;

But, under your question above here on this EE page, it says  
Author:
david_m_jacobson, Premium Service Member
Time Zone:
Eastern Standard Time (GMT-05:00)


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20112538
>>How do I make sure that I get the right offset for any timezone?

The offset is contained in the TimeZone itself
0
 

Author Comment

by:david_m_jacobson
ID: 20112696
All right.  I feel like I am going in circles here.  My timezone might say GMT -5:00 but I can tell you that because of daylight savings time, right now it's GMT -4:00 in the East Coast of the US right now.  So if I call TimeZone.getOffset() and the Javadocs tell me that it's going to take into consideration daylight savings, why doesn't it take into consideration daylight savings?

Btw, both versions of getOffset() return -5:00 GMT instead of -4:00 GMT.
0
 
LVL 28

Expert Comment

by:rrz
ID: 20112970
It works for me.
TimeZone tzEST = TimeZone.getTimeZone("EST");  
int offset = tzEST.getOffset(new Date().getTime()) / 3600000;     //  I get -4 on my machine.
boolean daylightTime = tzEST.inDaylightTime(new Date());          //  I get true.
0
 
LVL 28

Expert Comment

by:rrz
ID: 20113038
What happens with the following ?
 SimpleDateFormat formater = new SimpleDateFormat("d MMMM yyyy");
 Date date = formater.parse("07 December 2007");
 TimeZone tzEST = TimeZone.getTimeZone("EST");
 int offset = tzEST.getOffset(date.getTime()) / 3600000;     //  I get -5 on my machine.
 boolean daylightTime = tzEST.inDaylightTime(date);          //  I get false.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20114115
>>but I can tell you that because of daylight savings time, right now it's GMT -4:00 in the East Coast of the US right now

Actually i'm not sure what i recommended  earlier is correct after all. The 3-letter IDs are in fact deprecated so that should be more like

TimeZone tz = TimeZone.getTimeZone("America/New_York");
System.out.printf("The offset from UTC in hours for %s is %d\n", tz.getID(), tz.getOffset(System.currentTimeMillis())  / (1000 * 60 * 60));

See http://java.sun.com/developer/technicalArticles/Intl/USDST_Faq.html and make sure you've run the tzupdater tool

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22486426

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
Accept a Comment: CEHJ {http:#20114115}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mwvisa1 Experts Exchange Cleanup Volunteer
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question