我的API给出了以下结果:
{"time":{"Hours":0,"Minutes":0,"Seconds":0,"Milliseconds":0,"Ticks":0,"Days":0,"TotalDays":0,"TotalHours":0,"TotalMilliseconds":0,"TotalMinutes":0,"TotalSeconds":0}}
如何获取Android中的hh:mm:ss格式?
发布于 2018-06-13 03:46:35
尝尝这个
public void getFormatedDate(JSONObject object){
try {
JSONObject object1 = object.getJSONObject("time");
int hour = object1.getInt("Hour");
int minute = object1.getInt("Minutes");
int seconds = object1.getInt("Seconds");
int milis = object1.getInt("Milliseconds");
String time = hour+":"+minute+":"+seconds+":"+milis;
} catch (JSONException e) {
e.printStackTrace();
}
}
发布于 2018-06-13 03:50:02
就这么简单-
String yourApiString = "";
try {
JSONObject jsonObject = new JSONObject(yourApiString); // Converts your API JSON string to JsonObject
JSONObject timeObject = jsonObject.getJSONObject("time"); // Fetch time object
int hours = timeObject.optInt("Hours"); // Fetches Hours integer
int minutes = timeObject.optInt("Minutes"); // Fetches Minutes integer
int seconds = timeObject.optInt("Seconds"); // Fetches Seconds integer
String time = hours + ":" + minutes + ":" + seconds; // Concatenate all values to get hh:mm:ss format
Log.d(TAG, "Time: " + time); // You will get time in hh:mm:ss
} catch (JSONException e) {
e.printStackTrace();
}
https://stackoverflow.com/questions/50824414
复制相似问题