2

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?


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 ?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Shreyas
  • 209
  • 1
  • 7
  • 16

2 Answers2

0

How about this

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

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

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
  • Thanks but "GreogorianCalendar" is not available in Blackberry JDE 4.5.0 API docs. – Shreyas Oct 29 '09 at 14:43
  • Well, you can call the variable Calendar, the general idea is the same as above. – Zoidberg Oct 29 '09 at 15:02
  • 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 Oct 29 '09 at 15:24
  • What about the getDSTSavings() function on the TimeZone object? Is that not available? – Zoidberg Oct 29 '09 at 15:41
  • getDSTSavings() is not available. public abstract boolean useDaylightTime() is there which checks if this time zone uses Daylight Savings Time. – Shreyas Oct 29 '09 at 16:08
  • 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 Oct 29 '09 at 16:47
0

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
  • 3,400
  • 1
  • 27
  • 41
  • Hey Thanks Chris. Its working. But getRawOffset() doesnt consider DayLight Savings Time (DST) & getOffset() does it. if i use getOffset then the result is coming 1 hr forward/backward for TimeZones which uses DST. (means in short for TimeZones using DST, Blackberry device is not using DST when I use getOffset(..)) So i think the Day Light Savings in disabled in my Blackberry Device. do u know how to enable it ? any settings to be done ? How to solve this issue for DST ? – Shreyas Oct 29 '09 at 14:35
  • 1
    (for eg. for "Adelaide" its time in Blackberry device is GMT +09:30 & if I use getOffset() then its coming +10:30) – Shreyas Oct 29 '09 at 14:39
  • 1
    Unfortunately, no, I was just looking at the javadoc, as Zoidberg probably was too. – Chris Oct 30 '09 at 12:42