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
Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Time Zone Issue?

Thank you Peter.  Sorry for the delay in responding.  I am in EST.

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

Re: Time Zone Issue?

And I am now finding that the time is 5 hours ahead of what it should be as opposed to the one hour behind issue I was experiencing.  What a pain this is.

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

Re: Time Zone Issue?

EST.  Should I be using

 

Calendar gmtCalendar = Calendar.getInstance(TimeZone.getDefault());

instead of

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

 



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

Re: Time Zone Issue?

OK, you still need to think about this.

 

Note:

- UTC is a fixed time, it does not know about DST, or care.

- UTC+5hours is still a fixed time.  It does not know about DST.  Adding 5 hours does not correct for Time Zone or DST.

- The code I have given you will output the time supplied, formatted as the UTC time.  It will not change with DST.

- You can not just add an offset to UTC time and expect it to be correct for a Time Zone.  The amount you have to add depends on whether DST is in force. 

 

I go back to one of the comments I made earlier.  If you want to Time that does not change, store it as a String value, like 10:20:11, not as a long. 

 

You say you are EST.  EST is effected by DST.  So you need to think about how it effects your processing. 

 

So let us take an example.

 

Firstly let us ignore the +5 hours.  I suspect that someone thought that if you did a System.currentMilliseconds() this would give the local time and they wanted to convert it to UTC.  But you get UTC time, the BlackBerry works in UTC time.  All the times you see are converted to match your Time Zone.  You change Time Zone, the times on your BB will change, but you will still get an increasing number from this method. 

 

So let us assume you store UTC time.

 

Say on November 5, EST is 4 hours behind UTC as DST changes on Nov 6 and the clocks go back.  Say you capture a new time, and you capture this time at 7:30 in the morning.  You format this time using the code I have given, assuming no adjustment (ignore the +5), I think this will be displayed as 11:30.

 

Now the clocks go back, which means that instead of being 4 hours behind UTC time, you are now 5 hours.  At 7:30 on November 6, you capture again.  And then you format it.  It will say 12:30. 

 

Is that what you want?

 

The time is not the problem here unfortunately, it is the collecting and the formatting that is the pain.  You need to be very clear on exactly what you are trying to collect and how it is going to be reported.  And these are two different but related issues. 

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

Re: Time Zone Issue?

it's not really that complicated ;-)

 

let's try to get a clear understanding first what you try to do:

 

you got a long that represents a specific time.

 

Now first question:

 

- Why do you add five hours to do?

- Do you want to format that time long value independent of what timezone is set on the device or dependent on it.

That's means: let's say the long time value represents Thu, Nov 10th 2011, 2pm GMT. Now do you want to make sure that formated string always says it's "Thu, Nov 10th 2011, 2pm GMT" even if the app runs on a BlackBerry that has its timezone set to EST or do you want it to show in the timezone of the BB, ie in case of EST it would show "Thu, Nov 10th 2011, 9am GMT". And the same time if it was in Berlin would show as "Thu, Nov 10th 2011, 3pm GMT"

 

We really can only help when we exactly knwo what you are trying to achieve. Once we know that, the rest is simple.

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

Re: Time Zone Issue?


peter_strange wrote:

 

You say you are EST.  EST is effected by DST. 

 


Just on a side note:

 

Peter, strictly speaking EST is also fixed. EST = GMT+5 and is not affected by DST.

It's the logical time zone name 'America/New_York' that is affected by DST.

 

All shortcut timezone names in Java are offsets to GMT and therefore are not affected by DST.

 

Basically the rule is:

 

In Winter: Europe/London == GMT or America/New_York == EST

In Summer: Europe/London == GMT + 1 or America/New_York == EST + 1

 

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

Re: Time Zone Issue?

[ Edited ]

Thanks for the reply.  Yes, I have a long value representing a specific date/time.  In some cases I need to add a few hours to that date/time (used as a stop date in a timer).  Forget about adding the time, what I need to do should be quite simple... take that long date/time value and display it as format yyyy-MM-dd hh:mm:ss aaa regardless of timezone and language on device. 

 

This works fine all the time and displays the date/time as Nov 10, 2011 10:08 AM...

Long someDateTime = (Long)dateTable.get("SomeDateTime");

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

Dialog.inform(dfSomeDateTime.toString());

 

However, I need that date/time to be returned as yyyy-MM-dd hh:mm:ss aaa.  And it needs to be displayed as yyyy-MM-dd hh:mm:ss aaa even when the language on the device is French or any other language.  Just can't get that working without losing hours or gaining hours.

 

Very much appreciate the assistance.  Thank you.







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

Re: Time Zone Issue?

Ok then in that case just go back a couple of posts and make sure that you get a TimeZone instance for GMT and use that with a calenar for your SimpleDateFormat. Since the long value is presented in GMT/UTC you have to tell the formatting class that you want to have it formatted/displayed in UTC/GMT. If you don't then it will, by default, format it in the default timezone, which is depends on the country setting of the user.

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

Re: Time Zone Issue?

Thanks.  Which post are you referring to?  I've been trying so many different things lately I'm getting confused.  :smileyhappy:

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

Re: Time Zone Issue?

it's basically the code from Peter. That will do what you want.

 

 

 

public static String formatGMTDateTime(long someDateTime) {
        StringBuffer sb = new StringBuffer(10);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");
        Date tempDate = new Date(someDateTime);
        Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone(DateTimeUtilities.GMT));
        gmtCalendar.setTime(tempDate);
        sb = sdf.format(gmtCalendar, sb, null);
        return sb.toString();
}