10-28-2009 04:44 AM
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 ?
Solved! Go to Solution.
10-28-2009 05:34 AM
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:
10-28-2009 11:31 AM - edited 10-28-2009 11:32 AM
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 ?
10-28-2009 11:43 AM - edited 10-28-2009 11:45 AM
"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! ![]()
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.
10-28-2009 11:50 AM
Yes I m using RIM APIs.
Thanks for the reply. I will try this one..
10-28-2009 12:11 PM - edited 10-28-2009 12:26 PM
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 ?
10-28-2009 01:12 PM
int gmtOffset_Min = (gmtOffset_Mili % DateTimeUtilities.ONEHOUR)/DateTimeUtilities.ONEMI
10-29-2009 01:34 AM
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 ?
10-29-2009 02:10 AM - edited 10-29-2009 03:00 AM
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 ?
10-29-2009 05:20 AM
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.