ven27 ven27 - 8 days ago 5
Android Question

Changing timezone to IST

I am programmatically trying to set the timezone to Indian Standard Time (IST) in Android, but nothing seems to work!

Here's the code snippet:

SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
// TimeZone.setDefault(TimeZone.getTimeZone("GMT +5:30"));
// TimeZOne.setDefault(TimeZone.getTimeZone("IST"));
Calendar c = Calendar.getInstance();
System.out.println(s.format(c.getTime()));

Answer

Rather than changing the default time zone (which you're doing after creating the SimpleDateFormat) you should just set the time zone of the SimpleDateFormat:

SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
s.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(s.format(new Date()));