gpt4 book ai didi

java - 获取给定时区的当前时间 : android

转载 作者:IT老高 更新时间:2023-10-28 23:26:44 28 4
gpt4 key购买 nike

我是 Android 新手,目前在给定时区的情况下获取当前时间时遇到问题。

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

是否有一种简洁的方法可以在上述给定时区中获取当前时间?任何帮助表示赞赏。谢谢,

编辑:尝试这样做:

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

最佳答案

我让它像这样工作:

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

另外,基于此日期的其他时间转换也应与此时区一起使用,否则将使用设备的默认时区,并且将基于该时区转换时间。

关于java - 获取给定时区的当前时间 : android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16202956/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com