Tweet
Greetings,
I was working on a logic in Blackberry application where I had to show time on screen in two different timezones together where one being the default IST timezone and another being PST timezone. At first I stuck but after reading couple of forum threads I could manage to do so. I am sharing the approach today.
First of we need to find list of available timezone and their IDs in order to make it work.
A simple code to get list of timezone available on Blackberry platform could be like this:
String[] supportedTimezoneIDs = TimeZone.getAvailableIDs();
for(int i=0; i<supportedTimezoneIDs.length; i++){
System.out.println(supportedTimezoneIDs[i]);
}
It will print list of timezones like:
Pacific/Kwajalein
Pacific/Midway
Pacific/Honolulu
America/Anchorage
America/Tijuana
America/Los_Angeles
America/Phoenix
America/Denver
America/Tegucigalpa
America/Tegucigalpa_2
America/El_Salvador
America/Regina
America/Chicago
America/Mexico_City
America/Mexico_City_2
America/Bogota
America/Indianapolis
America/New_York
America/Caracas
America/La_Paz
America/Manaus
America/Santiago
America/Halifax
America/St_Johns
America/Montevideo
America/Guyana
America/Buenos_Aires
America/Sao_Paulo
America/Godthab
America/South_Georgia
Atlantic/Cape_Verde
Atlantic/Azores
GMT
Europe/Dublin
Africa/Luanda
Europe/Amsterdam
Europe/Belgrade
Europe/Brussels
Europe/Belgrade Yugoslavia(YU)
Africa/Windhoek
Asia/Amman
Africa/Harare
Asia/Jerusalem
Europe/Minsk
Africa/Cairo
Asia/Beirut
Europe/Athens
Europe/Helsinki
Asia/Kuwait
Africa/Nairobi
Asia/Baghdad
Europe/Moscow
Asia/Tehran
Asia/Tbilisi
Asia/Muscat
Asia/Baku
Asia/Yerevan
Asia/Caucasus
Asia/Kabul
Asia/Karachi
Asia/Tashkent
Asia/Yekaterinburg
Asia/Calcutta
Asia/Colombo
Asia/Katmandu
Asia/Dhaka
Asia/Almaty
Asia/Rangoon
Asia/Bangkok
Asia/Krasnoyarsk
Asia/Hong_Kong
Asia/Kuala_Lumpur
Australia/Perth
Asia/Taipei
Asia/Irkutsk
Asia/Tokyo
Asia/Seoul
Asia/Yakutsk
Australia/Darwin
Australia/Adelaide
Pacific/Guam
Asia/Vladivostok
Australia/Hobart
Australia/Brisbane
Australia/Sydney
Asia/Magadan
Pacific/Fiji
Pacific/Auckland
Pacific/Tongatapu
To get default timezone of the device:
System.out.println("Default TimeZoneID: "+Calendar.getInstance().getTimeZone().getID());
It will print the timezone ID that you can use for further conversion.
Now here is my logic in which I first check if the default timezone is IST, then I want a converted PST time or vice versa.
long timestamp = System.currentTimeMillis();
Calendar defaultCalendar = Calendar.getInstance(); //it gives time in default timezone set on device.
DateFormat dtFormatter = new SimpleDateFormat("E, yyyy-MM-dd HH:mm");
if(defaultCalendar.getTimeZone().getID().trim().equalsIgnoreCase("Asia/Calcutta")){
//This is now PST calendar instance
Calendar pstCal = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
// set the curent time, this need to be currenttimemills since in BB and Java
// this is a number of milisecond from epoch. When it is set in calendar with
// timezone, it will pick the date time of that timezone.
pstCal.setTime(new Date(System.currentTimeMillis()));
// this is another tricky part I found. Using format and passing the calendar
// instance is what gives the right result.
// I tried using formatLocal but it always gives datetime as per default timezone
// of the system and as in my case it was always IST, so no conversion
// happens for PST.
String formattedDate2 = dtFormatter.format(pstCal, new StringBuffer(), null).toString(); //dtFormatter.formatLocal(timestamp);
dateTimeField2.setText(formattedDate2 + " PST");
String formattedDate1 = dtFormatter.formatLocal(timestamp);
dateTimeField1.setText(formattedDate1 + "IST");
} else if(defaultCalendar.getTimeZone().getID().equalsIgnoreCase("America/Los_Angeles")){
Calendar istCal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
istCal.setTime(new Date(System.currentTimeMillis()));
String formattedDate2 = dtFormatter.format(istCal, new StringBuffer(), null).toString();//dtFormatter.formatLocal(timestamp);
dateTimeField2.setText(formattedDate2+" IST");
TimeZone tz = TimeZone.getDefault();
String formattedDate1 = dtFormatter.formatLocal(timestamp);
dateTimeField1.setText(formattedDate1+ "PST");
}
So this is how I converted the IST time into PST on Blackberry. I hope this would be of some help to people looking for the same.
Thanks,
Sameer.
Tweet


