The Artima Developer Community
Sponsored Link

Java Answers Forum
The TimeZone Class

1 reply on 1 page. Most recent reply: Apr 2, 2004 3:05 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

The TimeZone Class Posted: Apr 2, 2004 2:25 AM
Reply to this message Reply
Advertisement
I've in the process of refactoring some duplicated code, part of which is:
TimeZone oldZone = TimeZone.getDefault();
TimeZone newZone = TimeZone.getTimeZone("Europe/London");
TimeZone.setDefault(newZone);
 
Date currentDate = Calendar.getInstance(newZone).getTime();
 
TimeZone.setDefault(oldZone);
The purpose of the code is to get the current time (using UK daylight saving time). The TimeZone instances aren't referred to anywhere else and the code works but, if possible I'd like to simplify as follows::
TimeZone newZone = TimeZone.getTimeZone("Europe/London");
 
            Date currentDate = Calendar.getInstance(newZone).getTime();
...without introducing artifacts down the line.

In the absence of the original programmer (and despite help from the Javadoc doc'n) my questions are:
1) The line - TimeZone.setDefault(newZone); - seems redundant since currentDate's initialization includes a reference to newZone anyway.
2) The final line - TimeZone.setDefault(oldZone); - performs housekeeping. If it wasn't there, might there be any adverse effects on new Calendar instances created elsewhere in the code?

I'm putting together some test code to try things out but would rather know how this TimeZone/Calendar stuff really works rather than how it appears to work inferred from my tests.

Thanks,
Vince.


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: The TimeZone Class Posted: Apr 2, 2004 3:05 AM
Reply to this message Reply
Well, I've done a few tests and the results don't seem illuminating.
I do need - TimeZone.setDefault(newZone); - but it doesn't appear to make any difference whether or not I include a timezone in the Calendar.getInstance() method.
Resetting to the original default timezone doesn't appear to be necessary once the Timezone objects have gone out of scope (but the setDefault method is static, so I'm not sure of the exact scope, so - to be on the safe side - I'll leave it in). Basically, I think I'll leave the code as is but encapsulate it in a method.
Date getTimestamp(String timeZone)
{
    TimeZone oldZone = TimeZone.getDefault();
    TimeZone newZone = TimeZone.getTimeZone(timeZone);
    TimeZone.setDefault(newZone);
 
    Date d = Calendar.getInstance().getTime();
 
    TimeZone.setDefault(oldZone);
 
    return d;
}
It's a kludge based on ignorance, but it passes the tests.

Vince.

Flat View: This topic has 1 reply on 1 page
Topic: Object Oriented system Previous Topic   Next Topic Topic: Reversing a String

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use