0

I'm building an Android app which queries the OpenWeatherMap API. For the most part everything works well. The problem is when I calculate unix timestamps provided by the feed. The timestamps are set according to GMT. Therefore if you live in London and check the json feed below for the current weather conditions in Tokyo, you would give misleading "Sunrise" information since the output shows Sunrise (unix value 1457298145) = Sun, 06 Mar 2016 21:02:25 GMT. The sunrise is set according to GMT or London time. How could I use the feed below to calculate the sunrise according to the target city's (Tokyo) local time instead of GMT? Is this possible to achieve this with the json feed below? The user can choose any city in the world for current weather information. The challenge is to then provide the sunrise information according to the city the user has chosen programmatically in Java.

Tokyo Current Weather Feed:

http://api.openweathermap.org/data/2.5/weather?id=1850147&appid=44db6a862fba0b067b1930da0d769e98

Feed Response:

    {
  "coord": {
    "lon": 139.69,
    "lat": 35.69
  },
  "weather": [
    {
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10n"
    }
  ],
  "base": "cmc stations",
  "main": {
    "temp": 285.91,
    "pressure": 1026.25,
    "humidity": 97,
    "temp_min": 285.91,
    "temp_max": 285.91,
    "sea_level": 1030.08,
    "grnd_level": 1026.25
  },
  "wind": {
    "speed": 1.17,
    "deg": 174.003
  },
  "rain": {
    "3h": 0.1475
  },
  "clouds": {
    "all": 56
  },
  "dt": 1457361634,
  "sys": {
    "message": 0.0048,
    "country": "JP",
    "sunrise": 1457298145,
    "sunset": 1457340136
  },
  "id": 1850147,
  "name": "Tokyo",
  "cod": 200
}

What method should I create so that

    public static String getSunriseTime(int timeStamp) {
    //What should I do here with the info from the feed above?
    }
CBA110
  • 1,072
  • 2
  • 18
  • 37

1 Answers1

0

You just need to set the time zone of the java Calendar object to Tokyo, or whatever. The following snippet worked for me:

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

 class Xxx {
     public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Asia/Tokyo"));
        calendar.setTimeInMillis(1457298145 * 1000L);
        System.out.println(calendar.toString());
    }
}
TAM
  • 1,731
  • 13
  • 18