获取当前时间在给定的时区:机器人机器人、时区、时间

由网友(℡倒不出的思念)分享简介:我是新来的Andr​​oid和我目前所面临的问题,以获得给定的时区当前时间。我得到时区,格式为GMT-7,即字符串。我有系统的时间。有没有干净的方式来获得当前时间在上面给出的时区?任何帮助是AP preciated。谢谢你,编辑:试图做到这一点:公共字符串的getTime(字符串时区){日历C = Calend...

我是新来的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));

阅读全文

相关推荐

最新文章