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
Contributor
christopherbecker
Posts: 28
Registered: ‎11-13-2012
My Device: none

Get time now, Subtract from old time, get days

[ Edited ]

I have this piece of code that doesn't seem to be working, it's giving me an error 104.

 

Date timenow = new Date(System.currentTimeMillis());
String timenowstr = new String(timenow.toString());
int timenowint = Integer.parseInt(timenowstr);

String timeposted = new String(1343084902); // this is standard php time(); from the DB
int timepostedint = Integer.parseInt(timeposted);

int subtracttime = timepostedint - timenowint;
int getdays = subtracttime/(60*60*24*2);
String stringdate = String.valueOf(subtracttime);

 

add(new LabelField(stringdate));

 

If I only parse through the date from the DB, then it works, but as soon as I add the other bits, it breaks.

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

Re: Get time now, Subtract from old time, get days

Once again, it is useful if you give us as much detail as you can, - telling us which statement is getting the error makes our job easier so we are more likely to help you. 

 

Time on BlackBerry is usually represented using a long - milliseconds since the Unix epoch - so I would work in longs rather than ints when doing date/time manipulation. 

 

php time() is of course seconds since the Unix Epoch, so needs to be multiplied by 1000 to make the same as BlackBerry time. 

 

Review your code to make sure this difference has been correctly managed. 

 

Also be aware that Daylight Saving can throw curve balls at date/time calculations when the dates involved span the time change. Not saying it effects this code, just saying it might.

 

Contributor
christopherbecker
Posts: 28
Registered: ‎11-13-2012
My Device: none

Re: Get time now, Subtract from old time, get days

I'll rather do it in my web service, it'll be more accurate and consistent.
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Get time now, Subtract from old time, get days

This code makes no sense:

Date timenow = new Date(System.currentTimeMillis());
String timenowstr = new String(timenow.toString());
int timenowint = Integer.parseInt(timenowstr);

 You have a long, build a date out of it, read that as a string and parse this as an int, or rather, try to
.If you check http://www.blackberry.com/developers/docs/7.1.0api/java/util/Date.html#toString() it does not return an integer string.

 

Usually it is the best to just use the long values until you want to display something on the interface.

Store the "time then" as a long as well, and subtract it.

You can use simple arithmetics like longValue/1000 for seconds, /60000 for minutes etc. Be aware of the different operators '/' and '%'

----------------------------------------------------------
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
Contributor
christopherbecker
Posts: 28
Registered: ‎11-13-2012
My Device: none

Re: Get time now, Subtract from old time, get days

thanks for the tip Simon, but I've already implemented this into my web service, and it's running smoothly.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Get time now, Subtract from old time, get days

don't hesitate to mark the thread as solved, then
----------------------------------------------------------
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