Question

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.

Was it helpful?

Solution

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;
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top