I am a beginner in android Programming and creating an app that displays weather information of Morning (6am),afternoon(12pm) and Night(9pm) of the same day. I am using JSON data from this website. http://api.openweathermap.org/data/2.5/forecast?q=kangra,india&appid=41352ca35dd1be72cc7d9f47351d1b41&units=metric The time stamp given in the link is in UTC and I am having trouble to display weather information according to local time.(I need weather info at 6am,12pm and 6 pm Local time) I'd appreciate a lot of anyone can help solve my problem.
-
Please include the relevant snippet of data in your post itself, rather than link. – Basil Bourque Oct 15 '16 at 21:44
-
Possible duplicate of [Unix epoch time to Java Date object](http://stackoverflow.com/questions/535004/unix-epoch-time-to-java-date-object) – Basil Bourque Oct 15 '16 at 21:48
2 Answers
You need to read the documentation of your data feed to know for sure the meaning of the data.
I am going to guess that the dt
field is a number of whole seconds since the epoch of 1970-01-01T00:00:00
in UTC.
Example data:
"dt":1476576000
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.ofEpochSecond ( 1476576000L );
instant.toString(): 2016-10-16T00:00:00Z
You can adjust that into any time zone. Specify a proper time zone name in the format of continent/region
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!). Apply a ZoneId
to get a ZonedDateTime
.
ZoneId zoneKolkata = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdtKolkata = instant.atZone( zoneKolkata );
zdtKolkata.toString(): 2016-10-16T05:30+05:30[Asia/Kolkata]
You can do so again for other time zones.
ZoneId zoneMontreal = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtMontreal = instant.atZone( zoneMontreal );
zdtMontreal.toString(): 2016-10-15T20:00-04:00[America/Montreal]
All three of these date-time objects (instant
, zdtKolkata
, zdtMontreal
) are all the very same moment, the same single point on the timeline. The only difference is viewing the wall-clock time of different regions.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 1
- 1

- 303,325
- 100
- 852
- 1,154
The date format above is an epoch
format (or Unix time).
To convert to java's Date
you can do (as from this SO answer)
Date expiry = new Date(Long.parseLong(date));
Being date
the int
or long
value returned from the api, like 1476576000

- 1
- 1

- 17,825
- 6
- 78
- 112