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
Accepted Solution

Inconsistent Time when setting calendar

With the following code I'm getting inconsistent time.

 

If I set the string to "12/15/2011 10:20 PM" I get back "12/15/2011 04:20 PM".

If I set the string to "12/15/2011 12:00 AM" I get back "12/15/2011 04:00 AM".

etc.

 

DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
java.util.Calendar d2 = java.util.Calendar.getInstance();
String date = "12/15/2011 10:20 PM";
int yy=0,mm=0,dd=0,hr=0,mi=0;
String ampm = date.substring(17,19);
mm = Integer.parseInt(date.substring(0, 2)) - 1; // january = 0
dd = Integer.parseInt(date.substring(3 ,5));
yy = Integer.parseInt(date.substring(6, 10));
hr = Integer.parseInt(date.substring(11,13));
mi = Integer.parseInt(date.substring(14,16));
d2.set(Calendar.YEAR, yy);
d2.set(Calendar.MONTH, mm);
d2.set(Calendar.DATE, dd);
d2.set(Calendar.HOUR_OF_DAY, hr);
d2.set(Calendar.MINUTE, mi);
d2.set(Calendar.SECOND, 0);
if (ampm.equals("AM"))
{
d2.set(Calendar.AM_PM, Calendar.AM);  
}
else
{
d2.set(Calendar.AM_PM, Calendar.PM);
}
long t2 = d2.getTime().getTime();
_testDateField = new DateField("Date: ", t2, df, DrawStyle.LEFT);
add(_testDateField);

 

Any thoughts?

 

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Inconsistent Time when setting calendar

Probably going to have to call _testDateField.setTimeZone() and put it to the user's TimeZone. Working with the calendar and date objects can sometimes be a b**ch when you're trying to convert between GMT and you're time.

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Inconsistent Time when setting calendar

Agree with jproffit: if you search the forum you will find quite a bit of traffic on this subject. I know i have answered the question several times, and posted code examples for adjusting the time.

 

 

 

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

Re: Inconsistent Time when setting calendar

Thanks RexDoug. I've done a ton of searching and can't find anything that solves this problem. Can you please point me to a solution you've provided in the past? Would really appreciate it. Thanks a lot.
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Inconsistent Time when setting calendar

If you check the difference in the time, I think you'll find that the delta is the same as your time zone, i.e. the number of hours between your time zone and GMT.

 

Is this not the case?

 

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

Re: Inconsistent Time when setting calendar

Could be the case. Just not sure about setting the time zone to fix this issue. Shouldn't the zone come from the zone set on the device? Any thoughts on what needs to be done to fix this?
Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Inconsistent Time when setting calendar

Is this a time zone issue?  How can you tell?  Why wouldn't the time zone remain consistent from setting/getting time?  How do I programatically set the time zone to get this to work properly?

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Inconsistent Time when setting calendar

Date and Calendar objects represent their time in GMT. Did you try doing DateField.setTimeZone(TimeZone.getDefault()); if you're trying to set it for now (this won't necessarily work if you are doing it for different dates).

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

Re: Inconsistent Time when setting calendar

Thanks.  I'm pulling the date/time from a string that has been previously set.  Sounds like DateField.setTimeZone(TimeZone.getDefault()); wont work in this case.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Inconsistent Time when setting calendar

How are you generating the date String?