• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1549
  • Last Modified:

Timezone.getOffset() not returning right value

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
david_m_jacobson
Asked:
david_m_jacobson
  • 8
  • 6
  • 4
  • +2
1 Solution
 
sciuriwareCommented:
EST = GMT-5

According to the Time Zone tables from Microsoft Windows XP.

;JOOP!
0
 
sciuriwareCommented:
You can also read this under your post!!!!!!!

;JOOP!
0
 
david_m_jacobsonAuthor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
sciuriwareCommented:
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
 
sciuriwareCommented:
And for the offset I suggest:

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

;JOOP!
0
 
david_m_jacobsonAuthor Commented:
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
 
sciuriwareCommented:
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
 
CEHJCommented:
I think you need to set your timezone thus:

TimeZone tz = TimeZone.getTimeZone("EST5EDT");
0
 
david_m_jacobsonAuthor Commented:
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
 
david_m_jacobsonAuthor Commented:
The above list is my current list of timezone strings.  Is this the correct list that will handle daylight savings appropriately?
0
 
CEHJCommented:
The default timezone is taken from the system
0
 
sciuriwareCommented:
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
 
sciuriwareCommented:
P.S.: daylight savings time enabled/disabled is shown in the dialog described above.

;JOOP!
0
 
david_m_jacobsonAuthor Commented:
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
 
sciuriwareCommented:
You got the right offset already, but you said it was not adjusted for daylight saving.

;JOOP!
0
 
rrzCommented:
>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
 
CEHJCommented:
>>How do I make sure that I get the right offset for any timezone?

The offset is contained in the TimeZone itself
0
 
david_m_jacobsonAuthor Commented:
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
 
rrzCommented:
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
 
rrzCommented:
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
 
CEHJCommented:
>>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
 
Kevin CrossChief Technology OfficerCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 8
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now