1

I got this JSON response by using OpenWeather's API:

{
"coord": {
"lon": 139.69,
"lat": 35.69
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 306.63,
"feels_like": 311.16,
"temp_min": 304.82,
"temp_max": 308.15,
"pressure": 1011,
"humidity": 63
},
"visibility": 10000,
"wind": {
"speed": 3.1,
"deg": 140
},
"clouds": {
"all": 75
},
"dt": 1598419499,
"sys": {
"type": 1,
"id": 8077,
"country": "JP",
"sunrise": 1598386129,
"sunset": 1598433465
},
"timezone": 32400,
"id": 1850144,
"name": "Tokyo",
"cod": 200
}

I wish to get the current time from that city by using timezone property or another property. Is it possible? how?

  • 1
    Does this answer your question? [How to convert DateTime into different timezones?](https://stackoverflow.com/questions/26257481/how-to-convert-datetime-into-different-timezones), look at the packages referred in this question – Yadu Aug 26 '20 at 05:39

2 Answers2

2

A way to get current time of the city using the timezone you've got from the API can be as following:

DateTime.now().add(Duration(seconds: timezone - DateTime.now().timeZoneOffset.inSeconds))

You will get a DateTime object from here.

shafayat hossain
  • 1,089
  • 1
  • 8
  • 13
-1

To solve your problem you have to convert your country code to country name and then do your JSON Code to solve your problem.

 TextView textView = findViewById(R.id.textView);

    Locale loc = new Locale("","JP");
    String country = loc.getDisplayCountry();

    //JSON Code

    TimeZone tz = TimeZone.getTimeZone(country + "/" + city);
l_b
  • 533
  • 5
  • 10
  • The city variable is the city name which you will get from JSON Code and then put it in as a String. – l_b Aug 26 '20 at 06:03