views:

290

answers:

1

Hi All,

I would like to know how to calculate difference between two different timezones / timestamps in Blackberry application.

For e.g. Calculate difference in server timestamp and client timestamp

Please help. Thanks.

+1  A: 

Try this code:

class Scr extends MainScreen {
 public Scr() {
  String time1 = "2009-11-27 01:38:05";
  String zone1 = "Pacific/Midway";
  String time2 = "2008-05-01 12:38:05";
  String zone2 = "MST";

  long timeDiff = getTimeDifference(time1, zone1, time2, zone2);
  Date date = new Date(timeDiff);
  add(new LabelField(String.valueOf(date)));
 }

 public long getTimeDifference(String timestamp1, String timezone1,
   String timestamp2, String timezone2) {
  long time1 = getTime(timestamp1, TimeZone.getTimeZone(timezone1));
  long time2 = getTime(timestamp2, TimeZone.getTimeZone(timezone2));
  return time2 - time1;
 }

 public long getTime(String time, TimeZone timeZone) {
  Date formatter = new Date(HttpDateParser.parse(time));
  int offset = timeZone.getRawOffset();
  return formatter.getTime() + offset;
 }
}
Max Gontar
Thanks for the answer, I would like to know how do I add one minute to current timestamp...
imMobile
Add 1000*60 to long value (it's in milliseconds)
Max Gontar
Hi, Can you please give me a source code example for adding a single unit of time to timestamp, just like add method of Java Calendar class. Thanks
imMobile
Post a question, this may be helpful for others!
Max Gontar