Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TimeZone insanity

Status
Not open for further replies.

rejefi

Programmer
Joined
Nov 22, 2004
Messages
3
Location
US
Hello, I'm attempting to find the difference between two dates, which (may) be in different timezones. One set of timezones will always be GMT; the others will vary. I simulate the problem in the following code:

Code:
TimeZone t1 = TimeZone.getTimeZone("PDT");
TimeZone t2 = TimeZone.getTimeZone("GMT");
Calendar c1 = Calendar.getInstance(t1);
Calendar c2 = Calendar.getInstance(t2);
long diff = c1.getTimeInMillis() - c2.getTimeInMillis();

diff is always 0, counterintuitively (at least to me).

What do I need to do to make this work?

Thanks
 
OK, google to the rescue. Here's the code. Ugggly.

Code:
TimeZone t1 = TimeZone.getTimeZone("PST");
TimeZone t2 = TimeZone.getTimeZone("GMT");
Calendar c1 = Calendar.getInstance(t1);
Calendar c2 = Calendar.getInstance(t2);
long diff = (c1.getTimeInMillis() + c1.getTimeZone().getOffset(c1.getTimeInMillis())) - (c2.getTimeInMillis() + c2.getTimeZone().getOffset(c2.getTimeInMillis()));   
long hoursDiff = diff / (60 * 60 * 1000);
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top