【问题标题】:How can I get date, time and day from open weather API in android [duplicate]如何从android中的开放天气API获取日期、时间和日期[重复]
【发布时间】:2018-10-21 03:40:09
【问题描述】:

我正在尝试使用纬度和经度从open weather API 获取特定位置和特定日期的日期、时间和日期。但它给了我一个类似1525974999 的长整数。如何从中检索日期时间和日期?

【问题讨论】:

标签: java android


【解决方案1】:

使用 Java 8 时间 API:

Instant.ofEpochSecond(1525974999) // returns: 2018-05-10T17:56:39Z

使用旧 Java Date:

new Date(1525974999 * 1000L) // returns: Thu May 10 13:56:39 EDT 2018

我在美国东部时区

【讨论】:

    【解决方案2】:

    整数表示自 1970 年 1 月 1 日以来经过的时间量。(Unix 时间戳)

    您可以使用来自 unix 时间戳的转换器,也可以仅以编程方式进行数学运算。

    【讨论】:

      【解决方案3】:

      可能在几秒钟内。试试这个:

      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(timeReturnedByAPI * 1000);
      
      int year = calendar.get(Calendar.YEAR);
      int month = calendar.get(Calendar.MONTH);
      int day = calendar.get(Calendar.DAY_OF_MONTH);
      

      这是日历 API:https://developer.android.com/reference/java/util/Calendar

      编辑:您可能希望使用需要时区来获取本地时间的 getInstance 版本。

      编辑 2:更新以响应 cmets。

      【讨论】:

      • 几秒钟之内
      • 但是如何从这里获取日期、时间和日期?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 2021-07-30
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      相关资源
      最近更新 更多