Get current time in a given timezone : android

I got it to work like this :

TimeZone tz = TimeZone.getTimeZone("GMT+05:30");
Calendar c = Calendar.getInstance(tz);
String time = String.format("%02d" , c.get(Calendar.HOUR_OF_DAY))+":"+
            String.format("%02d" , c.get(Calendar.MINUTE))+":"+
.                   String.format("%02d" , c.get(Calendar.SECOND))+":"+
    .           String.format("%03d" , c.get(Calendar.MILLISECOND));

Also, every other time conversion based on this date should also be used with this timezone, otherwise, the default timezone of device will be used and the time will be converted based on that timezone.


// Backup the system's timezone
TimeZone backup = TimeZone.getDefault();

String timezoneS = "GMT-1";
TimeZone tz = TimeZone.getTimeZone(timezoneS);
TimeZone.setDefault(tz);
// Now onwards, the default timezone will be GMT-1 until changed again

Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String timeS = String.format("Your time on %s:%s", timezoneS, date);
System.out.println(timeS);

// Restore the original timezone
TimeZone.setDefault(backup);
System.out.println(new Date());