【问题标题】:How can i know time in a specific country if i am in a differenet country?如果我在不同的国家,我如何知道特定国家的时间?
【发布时间】:2014-06-07 03:42:49
【问题描述】:

我正在尝试这样做 1.获取我的时间当前时间和时区。 2. 我从谷歌知道那个特定国家的时区。 3. 计算时区差异。 4. 从当前时间减去这个差值。这将使我有时间在其他国家。 我被困在第 3 步和第 4 步。 我像这样得到当前时间

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

【问题讨论】:

  • java.util.Date dateIn = new java.util.Date(); DateTime dateTimeUtc = new DateTime( dateIn, DateTimeZone.UTC );这给了我 DateTime 无法解析为类型的错误。
  • 在 StackOverflow 上多次提问和回答。搜索“Joda DateTimeZone”或“ZonedDateTime”。
  • 那段代码引用了 Joda-Time,一个优秀的日期时间库。所以你需要在你的项目中包含这个jar,并为org.joda 包添加import 语句。我们使用 Joda-Time 是因为与 Java 捆绑在一起的 java.util.Date 和 .Calendar 类非常麻烦,应该避免使用。
  • 谢谢,太棒了。

标签: java android datetime timezone


【解决方案1】:

你可以使用Joda-Time库,

Joda-Time User Guide

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

How can i know time in a specific country if i am in a differenet country?

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

您可以根据 Joda Time 中定义的 Canonical ID 获取 DateTimeZone,

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

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

因此,如果您想在此时获取纽约的当地时间,您可以执行以下操作

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

这里解释得非常完美,这是我对你的问题的参考

Get time of different Time zones on selection of time from time picker

您也可以使用此链接而不使用 api

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

其他参考

How to get time with respect to another timezone in android

Date and time conversion to some other Timezone in java

How to convert date time from one time zone to another time zone

【讨论】:

    【解决方案2】:

    x 是时区 id,有一个它们列表的链接。只需向 gettimeZone("with the time zone id") 提供您想知道时间的城市的时区(我说的是城市,因为有些国家/地区有很多时区,例如美国或俄罗斯)。

    时区 tz = TimeZone.getTimeZone("x")

    http://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2011-02-20
      • 2012-10-14
      • 2020-05-22
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多