Get current time in a given timezone : android

后端 未结 10 1968
悲哀的现实
悲哀的现实 2020-11-30 10:13

I am new to Android and I am currently facing an issue to get current time given the timezone.

I get timezone in the format \"GMT-7\" i.e. string. and I have the sys

10条回答
  •  迷失自我
    2020-11-30 10:41

    Set the timezone to formatter, not calendar:

    public String getTime(String timezone) {
        Calendar c = Calendar.getInstance();
        Date date = c.getTime(); //current date and time in UTC
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        df.setTimeZone(TimeZone.getTimeZone(timezone)); //format in given timezone
        String strDate = df.format(date);
        return strDate;
    }
    

提交回复
热议问题