获取当前时间在给定的时区:机器人 [英] Get current time in a given timezone : android

查看:195
本文介绍了获取当前时间在给定的时区:机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和我目前所面临的问题,以获得给定的时区当前时间。

我得到时区,格式为GMT-7,即字符串。 我有系统的时间。

有没有干净的方式来获得当前时间在上面给出的时区? 任何帮助是AP preciated。 谢谢你,

编辑: 试图做到这一点:

 公共字符串的getTime(字符串时区){
    日历C = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone(时区));
    日期日期= c.getTime();
    SimpleDateFormat的DF =新的SimpleDateFormat(DD / MM / YYYY);
    字符串strDate = df.format(日期);
    返回c.getTime()的toString()。
}
 

解决方案

我得到它的工作是这样的:

 时区TZ = TimeZone.getTimeZone(GMT + 05:30);
日历C = Calendar.getInstance(TZ);
字符串时间=的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));
 

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 system time.

Is there a clean way to get the current time in the above given timezone? Any help is appreciated. Thanks,

edit : Trying to do this :

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

解决方案

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));

这篇关于获取当前时间在给定的时区:机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆