请求日期显示解决方案

时间:2012-03-22 14:23:04

标签: android date textview

final String message [] = {“”,“”,“”};

    try{
        String UID = null, UBAL = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar PDate;


        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("UserID",id.getId()));

        response = connection.executeHttpPost("http://condoproject.net16.net/PayCheck.php", postParameters);
        Toast.makeText(getApplicationContext(), ""+response, Toast.LENGTH_LONG).show();

        JSONArray jArray = new JSONArray(response);

        for(int i = 0; i<jArray.length();i++)
        {
            JSONObject json_data= (JSONObject) jArray.get(i);

            Toast.makeText(getApplicationContext(), ""+json_data, Toast.LENGTH_LONG).show();

            UID = json_data.getString("UserID");
            UBAL = json_data.getString("UPayment");

            Uid1.setText(UID);
            ubal.setText(UBAL);

            PDate = (Calendar) json_data.get("PayDate");
            PDate.add(Calendar.MONTH, 1);               

            String P = ""+PDate;
            Udate.setText(P);


        }

可以显示UserID和Balance,但仅适用于日期textview为空。我可以知道解决方案吗?

2 个答案:

答案 0 :(得分:0)

“PayDate”的格式是什么?您无法将其强制转换为Calendar对象。请参阅documentation。也许你打算写:

Udate.setText(json_data.getString("PayDate"));

答案 1 :(得分:0)

这可能是错误的:

PDate = (Calendar) json_data.get("PayDate");
PDate.add(Calendar.MONTH, 1);  
String P = ""+PDate;

首先,我怀疑从JSONObject到Calendar的这种方式。 “值可以是JSONObjects,其他JSONArrays,字符串,布尔值,整数,长整数,双精度,null或NULL的任意组合。值可能不是NaN,无穷大或此处未列出的任何类型。” (here

尝试:

String myDateString = json_data.getString("PayDate"); // you can get a date as String

其次,你不能用这种方式得到人类可读格式的日期:

String P = ""+PDate;

如果您想在日期中添加内容,则必须转换并转换回来,例如:

// assuming that the format of your date is "yyyy-MM-dd"
//convert from String to Date
Date myDate = dateFormat.parse(myDateString);
//convert from Date to Calendar
PDate = Calendar.getInstance();
PDate.setTime(myDate);
//this adds 1 month to your Calendar object:
PDate.add(Calendar.MONTH, 1);
//this converts back from Calendar to Date object
myDate = PDate.getTime();
//this converts from Date to String
myDateString = dateFormat.format(myDate);

// and update text
Udate.setText(myDateString );

如果您只向PDate添加一个月以补偿它存储从0到11的月值的事实,那么您可以按 Dheeraj 表示