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
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Time Zone Issue?

DateFormat is locale-independant, btw, only SimpleDateFormat is locale-dependant.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Time Zone Issue?

Here is an example of the problem I am facing...

 

 

This works fine and produces the correct date/time as Nov 10, 2011 1:47 PM...

 

DateField dfSomeDateTime = new DateField("", someDateTime.longValue(), DateField.DATE_TIME);

Dialog.inform("df: " + dfSomeDateTime.toString());

 

This produces the date in the proper format but 7 hours behind (2011-11-10 06:47:58)...

 

StringBuffer sb = new StringBuffer(10);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");

Date tempDate = new Date(someDateTime.longValue());

Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone(DateTimeUtilities.GMT));

gmtCalendar.setTime(tempDate);

sb = sdf.format(gmtCalendar, sb, null);

Dialog.inform("sb: " + sb.toString());

 

 

Any thoughts as to why?

 

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Time Zone Issue?

If that's the case then the long value you have stored is, when baselined with UTC, 7 hours out of what it should be. So add 7 hours to it.

 

It's important that you make sure, when storing the long values, that they are UTC base lined, which is normally the case if you get them using the Date or Calendar classes. You have to be consequent with their use. Don't try to be clever and manipulate the long yourself.

 

 

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Time Zone Issue?

I'm setting the long value as

long someDateTime = System.currentTimeMillis();

 

Shouldn't System.currentTimeMillis(); be UTC?  Can I make it UTC?

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Time Zone Issue?

System.currentTimeMillis() always returns the time in UTC/GMT.

Double check everything. What else do you do with this long value before you store it?

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Time Zone Issue?

The only thing I do with this long value before storing it is sometimes add a couple hours like so...

 

long someDateTime = System.currentTimeMillis() + (5 * DateTimeUtilities.ONEHOUR); //now + 5 hrs

dateTable.put("SomeDateTime", new Long(someDateTime));

 

That about code should be fine.  Can;t see what would be wrong here.

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Time Zone Issue?

I will get some code together later this afternoon.

It definetly will work. As I said, it's not that complicated. You just have to be consequent in the use of the right classes :-)
Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Time Zone Issue?

Thank you.  I appreciate the assistance.  My code looks fine and should work... just not sure why it isn't.  Look forward to hearing from you.

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Time Zone Issue?

I think you really need to think about what you want.  It is tricky.  Some things are adjusted according to locale, something's are not.  Some times you want to be adjusted, some you don't.  Some you want to at fixed time in the day regardless of where you are, some you don't.

 

Take another example, I am booking an appointment.  The meeting is with a colleague in San Francisco.  I am in the UK.  The meeting takes place at 9:00.  I put that time in my Calendar.  My colleague also puts that time in his Calendar, but he lives in SF.  Now convert these times to UTC time.  According to UTC time (remember I am in the UK), we will miss each other by 8 hours.  But when I fly to SF, my BlackBerry adjusts the time it displays to match the local time zone. 

 

But also there is a big game I want to watch, in NY at 5:30 in the evening.  I can't put this in my Calendar can I?  I don't want this time to be 'local time'. 

 

So what do you want.  Tell us what you are trying to achieve and perhaps we can tell you what you need to do.

 

Responding to the post that begins:

 

"Here is an example of the problem I am facing..."

 

Remember also that DateField is adjusted to local time.  You can give it a Time Zone, and you give it a SimpleDateFormat.  If you give it GMT (which is close enough to UTC to not matter) and tell it to use this format:

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");

it will display the same as the code. 

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Time Zone Issue?

Thanks Peter.  I see what you are saying and understand.  All I need my app to do is get the current date time, add a couple hours to it then display it as yyyy-MM-dd hh:mm:ss aaa.  No concern for the time zone or dst, etc.  Users wont be travelling or comparing to anything other than the datetime on their own device.  Any thoughts as to why my "Here is an example of the problem I am facing..." code isn;t working as expected?  Thanks a lot.