【问题标题】:How to format the date in this android app如何在这个 android 应用程序中格式化日期
【发布时间】:2014-04-24 06:07:15
【问题描述】:

现在在我的 Android 应用中,“创建”日期采用以下格式:2014-02-21 00:00:00

我希望它采用以下格式:2014 年 2 月 21 日 00:00:00

如果可以用这段代码重新格式化它,有人可以告诉我怎么做吗?

@Override
protected void onPostExecute(String sJson) {
    if(sJson == null) {
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        

    try {
        // convert json string to json array
        JSONArray aJson = new JSONArray(sJson);
        // create apps list
        List<Application> apps = new ArrayList<Application>();

        for(int i=0; i<aJson.length(); i++) {
            JSONObject json = aJson.getJSONObject(i);
            Application app = new Application();
            app.setTitle(json.getString("app_title"));
            app.setDesc(json.getString("description"));               
            app.setCreator(json.getString("creator"));
            app.setCreated(json.getString("created"));
            app.setRank(json.getString("rating"));  
            app.setIcon(json.getString("icon"));

            // add the app to apps list
            apps.add(app);
        }

        //notify the activity that fetch data has been complete
        if(listener != null) listener.onFetchComplete(apps);
    } catch (JSONException e) {
        msg = "Invalid response";
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        
}

【问题讨论】:

  • 你不认为你可以很容易地找到类似的问题吗?

标签: android json date format getstring


【解决方案1】:

尝试以下操作:

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    SimpleDateFormat sdf1=new SimpleDateFormat("MMM dd yyyy hh:mm:ss");
    app.setCreated(sdf1.format(sdf.parse(json.getString("created"))));

【讨论】:

    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多