How to get IST time zone in Android

烈酒焚心 提交于 2020-01-03 02:00:49

问题


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


回答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!




回答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



来源:https://stackoverflow.com/questions/30416343/how-to-get-ist-time-zone-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!