I have looked everywhere but I couldn't find anything about parsing a date time json object in android.
I am trying to convert this JSON 2016-08-12T20:07:59.518451
to get ONLY the time like this 20:07
and format it in the correct time zone UTC/GMT +1 hour.
I could do it in javascript, but I wasn't able to get it right in Java/Android.
Is there a method that handle this for me or will I need to use Regex to get the correct time?
EDIT: here is the code. expectedArrival is the one with the json date/time and I only want to get the time with UTC/GMT +1 hour time zone.
public class JSONTaskArrivals extends AsyncTask<String, String,List<ArrivalItem>> {
@Override
protected List<ArrivalItem> doInBackground(String... params) {
//json
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
buffer.append(line);
}
String finalJSON = buffer.toString();
JSONArray parentArray = new JSONArray(finalJSON);
List<ArrivalItem> arrivalItems = new ArrayList<>();
int time = 0;
for (int i = 0; i < parentArray.length(); i++){
JSONObject finalObject = parentArray.getJSONObject(i);
ArrivalItem item = new ArrivalItem();
item.setDestination(finalObject.getString("destinationName"));
item.setEstimated(finalObject.getString("expectedArrival"));
time = finalObject.getInt("timeToStation")/60;
if(time < 1){
item.setLive("Due");
}else{
item.setLive(Integer.toString(time)+" mins");
}
arrivalItems.add(item);
}
return arrivalItems;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null){
connection.disconnect();
}
try {
if (reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<ArrivalItem> result) {
super.onPostExecute(result);
ArrivalsAdapter adapter = new ArrivalsAdapter(ArrivalsActivity.this, R.layout.arrivals_row, result);
lv = (ListView) findViewById(R.id.arrivals_listView);
lv.setAdapter(adapter);
}
}