1

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
    }
aminography
  • 21,986
  • 13
  • 70
  • 74
Dens
  • 125
  • 11
  • It seems that there is an assumption that the offset doesn’t change between sunrise and sunset. While most changes happen in the night and I don’t readily know a counter-example, it still seems a dangerous assumption to me. – Ole V.V. Sep 26 '19 at 09:19
  • Looking at the clarity of requirement, this is a useful question (although not a great question as OP has not posted any attempt). And of course, the brilliant answer (I wish I could upvote that more than once) by @OleV.V. has made this page highly referenceable for future visitors. Just to keep the spirit of posting clear requirement, my +1 for this question. – Arvind Kumar Avinash Oct 01 '20 at 18:39

1 Answers1

3

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

Table of which java.time library to use with which version of Java or Android.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    I’m doing something wrong every time I answer a question about date and time on Android. I always try to explain that you need ThreeTenABP, and people always tell me back that it doesn’t work below API level 26. Would it be possible that you read the section *Question: Can I use java.time on Android below API level 26?* once more and tell me how I can improve?? – Ole V.V. Sep 26 '19 at 09:58
  • [Me too.](https://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android/36168941#comment90582842_36168941) I suppose some people process graphics more easily than text. So, with that in mind, I made a table showing how to source the *java.time* technology. I posted to your Answer here. Delete if you dislike or feel it is too much. Feel free to copy and revise the idea if you have a better portrayal. Or reuse if you like it. This graphic is only 25K, fyi, so quick to load. – Basil Bourque Sep 28 '19 at 04:39