Cyborg91 Cyborg91 - 1 year ago 62
JSON Question

Regading about JSON data parsing in Android

I am using openweather API for calling current location.In JSON parsing, I saw

"dt": 1457852143
which means last updated.So in my app, I stored that value in String variable
String last_update=total.getString("dt")
where total holding the full JSON data.I ran my app and got the output as 1457852143 on screen.Although I wanted it to show in local time but I am unable to convert.
I am using Android Studio 1.5.Any suggestion would be highly appreciated.

Answer Source

I've used the following before to do this. This is using the Date class but I've heard it's better to use the Calendar class. This should get you in the right direction though.

    public static String convertUTCtoLocalTime(String p_city, String p_UTCDateTime) throws Exception{

String lv_dateFormateInLocalTimeZone="";//Will hold the final converted date
Datelv_localDate = null;
Stringlv_localTimeZone ="";
SimpleDateFormat lv_formatter;
SimpleDateFormat lv_parser;

//Temp for testing(mapping of cities and timezones will eventually be in a properties file
}else if(p_city.equals("NBI")){
}else if(p_city.equals("BRS")){
}else if(p_city.equals("MNT")){
}else if(p_city.equals("LAS")){

//create a new Date object using the UTC timezone
lv_parser = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
lv_localDate = lv_parser.parse(p_UTCDateTime);

//Set output format - // prints "2007/10/25  18:35:07 EDT(-0400)"
lv_formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z'('Z')'");
System.out.println("convertUTCtoLocalTime "+p_city+": "+ "The Date in the UTC time zone(UTC) " + lv_formatter.format(lv_localDate));

//Convert the UTC date to Local timezone
lv_dateFormateInLocalTimeZone = lv_formatter.format(lv_localDate);
System.out.println("convertUTCtoLocalTime: "+p_city+": "+"The Date in the LocalTime Zone time zone " + lv_formatter.format(lv_localDate));

return lv_dateFormateInLocalTimeZone;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download