如何将纪元时间转换为日期和时区以毫秒为单位的偏移量定义 [英] How do I convert epoch time to date and time zone is defined in offset in millisecond

查看:28
本文介绍了如何将纪元时间转换为日期和时区以毫秒为单位的偏移量定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个与天气应用相关的 api 日期.

这里我想将时间转换为日期时间格式.

我得到的日出时间是纪元时间.它是 1569494827(输入时间).我想把这个时间转换成一个日期.它的偏移时间以毫秒为单位-14400(以毫秒为单位的偏移量).

这次是纽约早上的时间.它的输出将是 06:47 AM但我无法将此纪元时间转换为输出时间

这是一个回复:我必须转换成日期

城市":{id":5128581,名称":纽约",坐标":{纬度":40.7306,lon":-73.9867},国家":美国",人口":8175133,时区":-14400,日出":1569494827,日落":1569538053}

解决方案

java.time 和 ThreeTenABP

 int offsetSeconds = -14_400;长sunriseSeconds = 1_569_494_827L;ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);即时sunriseInstant = Instant.ofEpochSecond(sunriseSeconds);OffsetDateTimesunriseDateTime =sridayInstant.atOffset(offset);System.out.println("日出:" + sundayDateTime);

输出为:

<块引用>

日出:2019-09-26T06:47:07-04:00

不用说日落也是如此.

避免使用 java.util.Date

如果我必须转换成日期,你的意思是java.util.Date:不要.那个班级设计很差,已经过时了,也不能保持抵消.最好使用 java.time.仅当对于尚未升级到 java.time 的遗留 API 需要 Date 时,才需要进行转换.在这种情况下,您可以完全忽略偏移量(时区"),因为 Date 无论如何都无法考虑它.只需转换我们得到的 Instant:

 Date oldfashionedDate = DateTimeUtils.toDate(sunriseInstant);System.out.println("作为老式日期对象:" + oldfashionedDate);

<块引用>

作为老式 Date 对象:2019 年 9 月 26 日星期四 12:47:07 CEST

Date 总是在 JVM 的默认时区打印,在我的例子中是中欧夏令时间 (CEST),这就是为什么我们没有在早上 6:47(它是一个非常烦人和令人困惑的行为).

问题:我可以在 API 级别 26 以下的 Android 上使用 java.time 吗?

是的,java.time 在新旧 Android 设备上运行良好.它只需要至少 Java 6.

  • 在 Java 8 及更高版本以及更新的 Android 设备(API 级别 26)中,内置了现代 API.在这种情况下,转换为:Date.from(sunriseInstant)(稍微简单一点).
  • 在 Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接).
  • 在(旧版)Android 上使用 ThreeTen Backport 的 Android 版本.它被称为 ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.

链接

  • I am parsing one api date related to the weather app.

    Here I am getting time in epoch time I want to convert into Date time format.

    I am getting sunrise time that is epoch time. It is 1569494827(input Time). I want to convert this time into a date. It has offset time in milliseconds -14400(offset in millisecond).

    This time is New York Morning time. Its Output will be 06:47 AM but I am unable to convert this epoch time to Output time

    This is a response: I have to convert into Date

    "city": {
            "id": 5128581,
            "name": "New York",
            "coord": {
                "lat": 40.7306,
                "lon": -73.9867
            },
            "country": "US",
            "population": 8175133,
            "timezone": -14400,
            "sunrise": 1569494827,
            "sunset": 1569538053
        }
    

    解决方案

    java.time and ThreeTenABP

        int offsetSeconds = -14_400;
        long sunriseSeconds = 1_569_494_827L;
    
        ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);
        Instant sunriseInstant = Instant.ofEpochSecond(sunriseSeconds);
        OffsetDateTime sunriseDateTime = sunriseInstant.atOffset(offset);
        System.out.println("Sunrise: " + sunriseDateTime);
    

    Output is:

    Sunrise: 2019-09-26T06:47:07-04:00

    It may be needless to say that sunset goes in the same fashion.

    Avoid java.util.Date

    If by i have to convert into Date you meant java.util.Date: don’t. That class is poorly designed and long outdated and also cannot hold an offset. You’re much better off using java.time. Only if you need a Date for a legacy API not yet upgraded to java.time, you need to convert. In this case you can ignore the offset ("timezone") completely since the Date cannot take it into account anyway. Just convert the Instant that we got:

        Date oldfashionedDate = DateTimeUtils.toDate(sunriseInstant);
        System.out.println("As old-fashioned Date object: " + oldfashionedDate);
    

    As old-fashioned Date object: Thu Sep 26 12:47:07 CEST 2019

    A Date always prints in the default time zone of the JVM, in my case Central European Summer Time (CEST), which is why we don’t get 6:47 in the morning (it’s a quite annoying and confusing behaviour).

    Question: Can I use java.time on Android below API level 26?

    Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. In this case the conversion is: Date.from(sunriseInstant) (a bit simpler).
    • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
    • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links

    这篇关于如何将纪元时间转换为日期和时区以毫秒为单位的偏移量定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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