首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JSON时间格式到Android时间格式

JSON时间格式到Android时间格式
EN

Stack Overflow用户
提问于 2018-06-13 03:23:29
回答 2查看 346关注 0票数 0

我的API给出了以下结果:

代码语言:javascript
复制
{"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格式?

EN

回答 2

Stack Overflow用户

发布于 2018-06-13 03:46:35

尝尝这个

代码语言:javascript
复制
     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();
    }

}
票数 0
EN

Stack Overflow用户

发布于 2018-06-13 03:50:02

就这么简单-

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50824414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档