我的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 格式?
答案 0 :(得分:0)
左右....
例如:
{"time":{"Hours":12,"Minutes":11,"Seconds":23,"Milliseconds":0,"Ticks":0,"Days":0,"TotalDays":0,"TotalHours":0,"TotalMilliseconds":0,"TotalMinutes":0,"TotalSeconds":0}}
我在eclipse中编写了这个,与android一样真的....
public static void main(String[] args) throws JSONException {
String jsonData = "";
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("/example.json"));
while ((line = br.readLine()) != null) {
jsonData += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
JSONObject object = new JSONObject(jsonData).getJSONObject("time");
int hour = object.getInt("Hours");
int min = object.getInt("Minutes");
int sec = object.getInt("Seconds");
System.out.println(hour + ":" + min + ":" + sec);
}
结果是......
12点11分23秒
答案 1 :(得分:0)
试试这个
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();
}
}
答案 2 :(得分:0)
简单 -
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();
}