Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified
Accepted Solution

How to get time 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");

 

 

So is there any way for this ?

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get time of current TimeZone ?

SimpleDateFormat will do this, except for the Time Zone which you will have to code for yourself.  

 

See this Thread for information on how to do this:

 

http://supportforums.blackberry.com/t5/Java-Development/Problem-with-SimpleDateFormat-TiemZone/m-p/3... 

Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to get time of current TimeZone ?

[ Edited ]

Thanks for the reply.

 

I m using Java 1.4 .

 

Using

 

TimeZone _timeZone = TimeZone.getDefault();
Calendar _calendar = Calendar.getInstance();
int 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));

I got

 

gmtOffset = 19800000

but I want it like

 

+0530 for TimeZone = Asia/Calcutta

So that I can write

 

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

I m using Java1.4 & in Java 1.5 u can write as

 

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

to get +0530

 

but How to get it in Java1.4 ?

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get time of current TimeZone ?

[ Edited ]

"I m using Java 1.4 "

i think you using the RIM Supplied APIs as they are what is supported on the BB device.  Either that or you are in the wrong forum!  :smileyhappy:

 

You have got the difference as 19800000.  This is in milliseconds.  You can use DateTimeUtilities to convert this to HH:mm format manually, for example, by dividing by DateTimeUtilities.ONE_HOUR to get the number of hours. 

 

There are other ways of formatting this, for example using SimpleDateFormat.formatLocal() with format HH:mm, but then you start having to worry about converting this to local time. 

 

I don't know of a way that is less likely to cause errors than doing this manually using DateTimeUtilities.

 

 

Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to get time of current TimeZone ?

Yes I m using RIM APIs.

 

Thanks for the reply. I will try this one..

Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to get time of current TimeZone ?

[ Edited ]

After doing like

 

 

int gmtOffset = 19800000
int gmtOffset_Hr = gmtOffset/DateTimeUtilities.ONEHOUR;

 

 

I got  "gmtOffset_Hr = 5 "

 

& by doing

 

 

int gmtOffset_Min = gmtOffset_Mili/DateTimeUtilities.ONEMINUTE;

 I got  "gmtOffset_Min = 330 "

 

I havent got Hour Value + or - &  Minute value is coming 330 instead of 30 (+0530 for Asia/Calcutta)

 

 

Why so ?

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get time of current TimeZone ?

int gmtOffset_Min = (gmtOffset_Mili % DateTimeUtilities.ONEHOUR)/DateTimeUtilities.ONEMINUTE;

Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to get time of current TimeZone ?

Hey Thanks its coming correct...

 

for gmtOffet_Hr & gmtOffset_Min & if its -3000 then the - sign is coming in gmtOffset_Hr..

 

I m getting it as

 

 

int gmtOffset_Hr = gmtOffset_Mili/DateTimeUtilities.ONEHOUR;   &
int gmtOffset_Min = (gmtOffset_Mili % DateTimeUtilities.ONEHOUR)/DateTimeUtilities.ONEMINUTE;

 

right ?

 

Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to get time of current TimeZone ?

[ Edited ]

But for some time zones ,

 

(for eg. for Australia/Adelaide, gmtOffset_Hr: 10 , gmtOffset_Min: 30 where as in Phone Settings time format for Adelaide is  +0930)

 

this issue is for 5-6 time zones..

 

I m using getOffset(..) of TimeZone class which considers Day Light savings ,

 

 

int gmtOffset_Mili = _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));

 

is it correct ?

 

By using getRawOffset(), its coming correct for Adelaide as  +0930 but getRawOffet() doesnt consider Day Light savings.

 

By using getRawOffset(), its coming correct for all TimeZones..

 

So how to handle for Day Light savings ?

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get time of current TimeZone ?

Probably the best way is to create two dates using a Calendar Object, one with local and one with GMT TimeZone, and subtract one from the other to get the difference.