views:

491

answers:

3

I m getting current time zone as ,

String defaultTimeZone = ""+TimeZone.getDefault();

Now I want to get its time for which I m using ,

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

for eg, if currentTimeZone = "Calcutta" then its time is +0530 which will be like ,

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss +0530");

I m using Java1.4 & RIM APIs , in Java 1.5 u can write as "yyyy-MM-dd HH:mm:ss Z" to get "+0530"

So how to do it in Java 1.4?

A: 

How about this

Calendar cal = Calendar.getInstance();
return cal.getTimeZone();

Gregorian calendar is pretty handy for all things date/time.

Zoidberg
Thanks but "GreogorianCalendar" is not available in Blackberry JDE 4.5.0 API docs.
Shreyas
Well, you can call the variable Calendar, the general idea is the same as above.
Zoidberg
Yes, using "Calendar" I got it working. Only the issue is for TimeZones using DST when I use getOffset(). Pls refer my comment above on this.
Shreyas
What about the getDSTSavings() function on the TimeZone object? Is that not available?
Zoidberg
getDSTSavings() is not available. public abstract boolean useDaylightTime() is there which checks if this time zone uses Daylight Savings Time.
Shreyas
What do you want to do with the DST, are you trying to standardize a time to one time zone? If so, why not instantiate a calendar object with the date/time's native timezone, then do a set timezone on the Calendar object, and see if it does it correctly.
Zoidberg
A: 

To be a little more specific, try this:

Calendar cal = Calendar.getInstance();
TimeZone timeZone = cal.getTimeZone();
int rawOffset = timeZone.getRawOffset();

The raw offset is in milliseconds. Divide by 3,600,000 to get the offset in hours.

Chris
I figured I would let the JavaDocs do the rest of the work.
Zoidberg
Shreyas
Shreyas
Unfortunately, no, I was just looking at the javadoc, as Zoidberg probably was too.
Chris
A: 

I checked using ,

boolean isDayLightSavings = _timeZone.useDaylightTime();
if(isDayLightSavings)
{    
  gmtOffset = _timeZone.getOffset(1, _calendar.get(Calendar.YEAR), _calendar.get(Calendar.MONTH), _calendar.get(Calendar.DATE), _calendar.get(Calendar.DAY_OF_WEEK), _calendar.get(Calendar.MILLISECOND));
}

but same result as its coming 1 hr forward/backward for TimeZones which uses DST. (ie. for TimeZones using DST, Blackberry device is not using DST when I use getOffset(..))

So should I enable DST in BB device. If yes then how to do it ?

Shreyas
Pretty sure calendar will know if the BB is using DST by getting the current time on it. As for how to set the BB to use DST, i would set it to Eastern Standard Time (Canada/US) as that time zone uses it for sure, while I believe Mountain (Chicago Illinois) does not.
Zoidberg
yes. u r right. can we enable DST in BB device ? means through any setting?
Shreyas