Shreyas
Shreyas

Reputation: 209

How to get time format of current TimeZone?

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 ?

Upvotes: 2

Views: 2752

Answers (2)

Zoidberg
Zoidberg

Reputation: 10323

How about this

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

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

Upvotes: 0

Chris
Chris

Reputation: 3519

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.

Upvotes: 0

Related Questions