如果我在一个不同的国家,我怎么知道特定国家的时间? [英] How can i know time in a specific country if i am in a differenet country?

查看:113
本文介绍了如果我在一个不同的国家,我怎么知道特定国家的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图这样做
1.获取我的时间当前时间和时区。
2.我知道Google特定国家/地区的时区。
3.计算时区差异。
4.从当前时间中减去这个差异。这将给我在其他国家的时间。
我被困在第3步和第4步。
我得到这样的当前时间

I am trying to do it this way 1. Get my time current time and time zone. 2. I know time zone of that specific country from Google. 3. Calculate difference in time zones. 4. Subtract this difference from current time. This will give me time in other country. I am stuck at step 3 and 4. I get current time like this

Date d = new Date();
CharSequence s = DateFormat.format("hh:mm:ss", d.getTime());

和这样的时区

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
            Locale.getDefault());
    Date currentLocalTime = calendar.getTime();
    SimpleDateFormat date = new SimpleDateFormat("Z");
    String localTime = date.format(currentLocalTime);

让特定国家/地区的时区由字符串x表示。现在我如何获得localTime -x和那么currentTime - (localTime - x)。

let time zone of specific country be represented by string x.Now how do i get localTime - x and then currentTime - (localTime - x).

推荐答案

你可以使用 Joda-Time 库,

Joda-Time用户指南

http://www.joda.org/joda-time/

如果我在不同国家/地区,我如何知道特定国家/地区的时间?

http://www.joda.org/joda-time/apidocs/index.html

您可以获取DateTimeZone dependi在Joda Time中定义的Canonical ID,

You can get the DateTimeZone depending on the Canonical ID defined in the Joda Time,

DateTimeZone zone = DateTimeZone.forID("Asia/Kolkata");

Joda Time有一个完整的Canonical ID列表,您可以从中获取TimeZone,具体取决于Canonical ID。

Joda Time has a complete list of Canonical ID from where you can get TimeZone depending on the Canonical ID.

所以,如果你想在纽约当地时间,你会做以下事情

So, if you want to get the local time in New York at this very moment, you would do the following

 // get current moment in default time zone
    DateTime dt = new DateTime();
    // translate to New York local time
    DateTime dtNewYork = dt.withZone(DateTimeZone.forID("America/New_York"));

这里解释非常完美,这是我对你的问题的引用

here it is explained very perfectly which is my reference to your question

从时间选择器中选择不同时区的时间

您也可以在不使用api的情况下使用此链接

and you can also use this link without getting use of api

http:// tutorials.jenkov.com/java-date-time/java-util-timezone.html

其他参考资料

如何获取关于android中另一个时区的时间

日期和时间转换为某些ot她在java中的时区

如何将日期时间从一个时区转换为另一个时区

这篇关于如果我在一个不同的国家,我怎么知道特定国家的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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