1

i have been parsing data from my weather i am successfully able to extract description,min and max temperature but date is in unknown format how to process the date to convert it into readable format

  "list":[  
  {  
     "dt":1497852000,
     "temp":{  
        "day":301.14,
        "min":294.81,
        "max":301.14,
        "night":294.81,
        "eve":301.14,
        "morn":301.14
     },
"pressure":990.68,
     "humidity":88,
     "weather":[  
        {  
           "id":501,
           "main":"Rain",
           "description":"moderate rain",
           "icon":"10d"
        }

My Code:

 public static void JSONParsing(String forecastJsonStr) throws JSONException {
    double MinTemp;
    double MaxTemp;
    String Date,description;
    JSONObject forecastDate = new JSONObject(forecastJsonStr);
    JSONArray ForecastData = forecastDate.getJSONArray("list");
    for(int i =0 ; i< ForecastData.length();i++){
        JSONObject weather = ForecastData.getJSONObject(i);
        JSONObject Data = weather.getJSONObject("temp");
        JSONObject Description = weather.getJSONArray("weather").getJSONObject(0);
         description = Description.getString("description");
        MinTemp = Data.getDouble("min");
        MaxTemp = Data.getDouble("max");


    }
}

2 Answers2

1

Your date is in seconds as long type so fetch the dt as long then multiply it with 1000 to convert it into miliseconds the use Date and SimpleDateFormat

1.) Fetch your Date as long

2.) Pass it to Date class constructor while multiplying it with 1000 to converts seconds into milisecond

3.) Create and apply SimpleDateFormat to get your date

e.g

    String s1 ="{\"dt\":1497852000}";
    JSONObject jsonObject2 = new JSONObject(s1);

    java.util.Date date = new java.util.Date(jsonObject2.getLong("dt")*1000);
    SimpleDateFormat date_format = new SimpleDateFormat("dd/MM/yy");
    String dateText = date_format.format(date);
    System.out.println(dateText); 

Output :

19/06/17

Note : Since your JSONResponse in incomplete so i just added a simple case demonstrating your issue

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • I too am stuck with this problem from numerous weather API's who return the date as Epoch time. What I want to be able to do is simply convert that string to a day name like "Monday", "Tuesday" etc is there an easy function in php to do this with the parsed JSON data? – MitchellK Feb 05 '19 at 07:23
  • @MitchellK you can use [`getdate`](https://stackoverflow.com/a/2475555/4936904) and use `$d['weekday']` to get the day, here the complete detail http://docs.php.net/getdate – Pavneet_Singh Feb 07 '19 at 01:11
  • 1
    Thanks @Pavneet_Singh I also found another nice way of formatting the date for my output if it helps anyone else in future. For the day name `$day1 = date('l', $jsondata['daily']['data'][0]['time']);` and for a nicely formatted date `$day1d = date(' (d-M-Y)', $jsondata['daily']['data'][0]['time']);` – MitchellK Feb 07 '19 at 07:20
0

You can format like this,

    try {
        JSONObject responseObject = new JSONObject(response);


        JSONArray listJsonArray = responseObject.optJSONArray("list");

        if (listJsonArray == null || listJsonArray.length() == 0) {
            return;
        }

        for (int i = 0; i < listJsonArray.length(); i++) {
            JSONObject eachDayJson = listJsonArray.optJSONObject(i);

            if (eachDayJson == null) {
                continue;
            }

            long dateInSeconds = eachDayJson.optLong("dt"); // Get date as seconds

            SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm");

            String readableDate = format.format(new Date(dateInSeconds * 1000)); // convert that in milliseconds

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41