Programming for the blackberry can be frustrating. It seems that they have taken every useful function I’ve used in the past and thought “is there a harder way to do this already?” If the answer was yes, they ripped out the function. The latest example I’ve uncovered while developing is simply adding time and dates to arrive at some time in the future. I am trying to add a number of seconds to an existing timestamp. In standard Java, there is a handy add function that does this for you. Unfortunately, in Java ME, they have ripped it out. To get that functionality back, you need to understand when you call getTime() on a date, it returns the number of milliseconds since January 1, 1970, 00:00:00 GMT as a long. The value “1″ represents one millisecond. So, to add one day to your date, you simply need to determine how many milliseconds in one day and add that to the value returned by getTime().
int milliseconds = 1 * 1000 * 60 * 60 * 24; // 1 day * 1000 milliseconds/second * 60 seconds/minute * 60 minutes/hour * 24 hours/day Date newDate = new Date(myDate.getTime() + milliseconds);
This will add one day to your date.
Post a Comment