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

Why Don't These 2 Dates Match? They Should...

I'm having a problem with dates.  In the following code, dialog.alert shows the correct date/time, however, the next dialog.alert adds a day and the time is wrong.  Any thoughts?

 

java.util.Calendar d2 = java.util.Calendar.getInstance(); // now
String date = "2010/12/12 14:01";
Dialog.alert(date); //returns 2010/12/12 14:01
int yy=0,mm=0,dd=0,hr=0,mi=0;
yy = Integer.parseInt(date.substring(0,4));
mm = Integer.parseInt(date.substring(5,7)) - 1; // january = 0
dd = Integer.parseInt(date.substring(8,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, hr);
d2.set(Calendar.MINUTE, mi);
long t2 = d2.getTime().getTime();
//convert long date to string
String strActivateDate = new Date(t2).toString();
Dialog.alert(strActivateDate); //return Mon Dec 13 02:01:07 GMT 2010

 

 

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Why Don't These 2 Dates Match? They Should...

[ Edited ]

using date.toString is using GMT, most likely this is the reason for the difference.

see the javadocs.

you can check DateFormat and SimpleDateFormat for timezone-related and unrelated formatting.

 

edit: corrected toString to GMT

----------------------------------------------------------
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
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Why Don't These 2 Dates Match? They Should...

Check your locate. I run and it works ok:

 

2010/12/12 14:01

Sun Dec 12 14:01:09 VET 2010

 

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

Re: Why Don't These 2 Dates Match? They Should...

I've checked my setting and they seem fine.  For some reason the code is still adding a day and changing the time.  However, it does seem t owork if I set the time to something before noon.  Strange.  Any ideas how this can be fixed so that it will run properyl?

 

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Why Don't These 2 Dates Match? They Should...

but you are aware of the concept (and existance) or timezones?

----------------------------------------------------------
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: Why Don't These 2 Dates Match? They Should...

Certainly aware of timezones.  Cant figure out why they are not the same.  Shouldnt they be taken from set timezone on device?  Why would .toString() change the time zone?

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Why Don't These 2 Dates Match? They Should...

[ Edited ]

getInstance() is using your timezone. toString from Date is using GMT. You are 12 hours behind, which is why before noon returns the same day. Date is not timezone aware, but Calendar is. You created the Date with a long, which is milliseconds and always GMT. You should check out SimpleDateFormat to pass in your Calendar object.

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

Re: Why Don't These 2 Dates Match? They Should...

Thanks fo the reply.  I very much appreciate it.  I see what you mean now.  I seem to need to create the date as a long in order for me to compare it to today's date/time.  I don't see a method to specify a date format for a long.  Is there some sort of convesion I need to take into consideration?  HttpDateParser perhaps?  Ultimately I need to create today's date/time to a string date/time.  I appreciate your feedback.  Thank you.

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

Re: Why Don't These 2 Dates Match? They Should...

In the following code I'm setting the d2 timezone equal to the d1 time zone yet it's still adding 12 hours...

 

String date = "2010/12/12 14:01";
Dialog.alert("a: " + date);

int yy=0,mm=0,dd=0,hr=0,mi=0;
yy = Integer.parseInt(date.substring(0,4));
mm = Integer.parseInt(date.substring(5,7)) - 1; // january = 0
dd = Integer.parseInt(date.substring(8,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, hr);
d2.set(Calendar.MINUTE, mi);
d2.setTimeZone(d1.getTimeZone());

long t1 = d1.getTime().getTime();
long t2 = d2.getTime().getTime();

//convert long date to string
String strCurrentDate = new Date(t1).toString();
String strActivateDate = new Date(t2).toString();
Dialog.alert("b: " + strActivateDate);

 

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Why Don't These 2 Dates Match? They Should...

[ Edited ]

No matter what you do, once you convert it from a Calendar to a long, you are going to lose the timezone and automatically convert it to GMT. The long is the milliseconds since the Epoch. When you call System.currentTimeMillis(), it is in GMT.

 

If all you are looking to do is take in a string and compare it to the current time, then yes, use HttpDateParser and compare the parse() response to System.currentTimeMillis(). If you want to compare just the day/month/year, then parse it and pass the long into a Calendar and compare the two Calendars.

 

This may also be useful to you: http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/util/DateTimeUtilities.html