How to get IST time zone in Android

10,223

Solution 1

import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        Calendar calender = Calendar.getInstance();

        calender.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));

        System.out.println(calender.get(Calendar.HOUR_OF_DAY) + ":" + calender.get(Calendar.MINUTE) +  ":" + calender.getActualMinimum(Calendar.SECOND));
    }
}

This is a full program that gets the time from the timezone in Asia/Calcutta. It displays hours, minutes and seconds!

Solution 2

You do not speak english, so lets talk Java. ;-)

 String format = "hh:mm:ss";
 SimpleDateFormat sdf = new SimpleDateFormat(format);
 sdf.setTimeZone(TimeZone.getTimeZone("IST"));
 System.out.format("%s\n", sdf.format(new Date()));

IST is India Standard Time or UTC/GMT + 5:30 h. I took this from the android API-Documentation mentioned in a comment of Der Golem (+1) and from the following link: http://www.timeanddate.com/time/zone/india/kolkata

Share:
10,223
user3930790
Author by

user3930790

Updated on June 26, 2022

Comments

  • user3930790
    user3930790 almost 2 years

    I am new to android application development , I am developing an app that is going to run in india only, I want to get the time of asia/kolkata timezone without using "joda" library I used the following code but i didn't get expected output

    Calendar cal = Calendar.getInstance();
    TimeZone tz = cal.getTimeZone();
    Log.d("Time zone","="+tz.getDisplayName());
    

    I want the time in hh:mm:ss format

    • Carcigenicate
      Carcigenicate almost 9 years
      @Biffen OMFG. That's literally the first thing I thought (exactly) when I read the title. That's awesome.
    • Phantômaxx
      Phantômaxx almost 9 years
    • user3930790
      user3930790 almost 9 years
      I am not good in english , any one pls help
    • user3930790
      user3930790 almost 9 years
      @DerGolem I could not get the correct time
  • user3930790
    user3930790 almost 9 years
    thanks it worked , your code is simple and easy @Bas