-4

I am able to Parse the JSON Data.There i got one Problem is that, on the Date Format there is also Default Time on JSON Data.Can Date and timebe splitted?

my JSON

[
  {
    "MasterID": "E0017",
    "StdID": 111,
    "Status": "U",
    "AmountPaid": 6645,
    "Class": 8,
    "DateOfReciept": "2017-01-01T00:00:00",***********hereis default time
    "Description": "[{\"des\":\"Admission\",\"Amount\":300},{\"des\":\"Monthly Fee\",\"Amount\":5400},{\"des\":\"Exam Fee\",\"Amount\":200},{\"des\":\"Extra Charge\",\"Amount\":400},{\"des\":\"Late Charge\",\"Amount\":345}]",
    "RecieptNo": 1011,
    "NAME": "Uzumaki Naruto",
    "recivedDate": "2017-03-10T00:00:00",
    "reciever": "Cynthia Irwin"
  }
]

getUsersListData()

 private void getUsersListData() {

        String URL = Navigation_URL + "?id=" + master_id;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            ArrayList<StudentFeeInformation> student_list = new ArrayList<>();
                            JSONArray jArray = new JSONArray(response);
                            //  studentFeeInformation = new StudentFeeInformation(response);
                            for (int i = 0; i < jArray.length(); i++) {

                                JSONObject jsonObject = jArray.getJSONObject(i);
                                String status = jsonObject.getString("Status");
                                String DateofReceipt = jsonObject.getString("DateOfReciept");
                                String ReceiptNumber = jsonObject.getString("RecieptNo");
                                String FeeReceivedDate = jsonObject.getString("recivedDate");
                                System.out.println("status:" + status);
                                student_list.add(new StudentFeeInformation(status, DateofReceipt, ReceiptNumber, FeeReceivedDate));
                            }
                            System.out.println("student_list size:" + student_list.size());
                            CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getActivity(), student_list);
                            listView.setAdapter(customFeeListStudentAdapter);


                        } catch (JSONException e) {

                            System.out.println("This is not good");

                            e.printStackTrace();

                        }

                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show();

            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                return headers;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);


    }

can it be possible to parse only certain value from the JSON Data?

Suman
  • 43
  • 11
seon
  • 1,050
  • 2
  • 13
  • 27
  • 1
    Just use a [.split("T")](https://www.w3schools.com/jsref/jsref_split.asp). The corresponding Date would be index 0. String DateTimeofReceipt = jsonObject.getString("DateOfReceipt"); String DateofReceipt = DateTimeofReceipt.split("T")[0]; – Isaiah Mar 15 '17 at 04:58

6 Answers6

2

Try this,

    String FeeReceivedDate = jsonObject.getString("recivedDate");       
    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    java.util.Date date = null;
    try
    {
        date = form.parse(FeeReceivedDate);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }
    SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd");
    String newDateStr = postFormater.format(date);

    Log.d("TAG","New date:"+newDateStr);
Komal12
  • 3,340
  • 4
  • 16
  • 25
1

Try this

String DateofReceipt = jsonObject.getString("DateOfReciept");
if(DateofReceipt.indexOf('T')!=-1)  //because indexOf() will return -1 if it is not found;
String date=DateofReceipt.substring(0, DateofReceipt.indexOf('T'));

Or you can use split

String DateofReceipt = jsonObject.getString("DateOfReciept");
String date=DateofReceipt.split("T",0)[0];
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
1

Try using substring()

String FeeReceivedDate = jsonObject.getString("recivedDate").substring(0,10);
BNK
  • 23,994
  • 8
  • 77
  • 87
Megha Maniar
  • 444
  • 5
  • 22
0

Problem is hear "DateOfReciept": "2017-01-01T00:00:00". Just replace T with blank space and use:

SimepleDateFormat strDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateOfReciept);
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
CrazyMind
  • 1,006
  • 1
  • 20
  • 22
0

you can use StringTokenizer class to split the string

 String CurrentString = "2017-03-10T00:00:00";
StringTokenizer tokens = new StringTokenizer(CurrentString, "T");
String date = tokens.nextToken();// this will contain "2017-03-10"
String time = tokens.nextToken();// this will contain "00:00:00"
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
-1

Yes. Definitely. But Parse with the same data type or convert the value to the same.

  1. For eg, In your code, receipt no. is of integer type and you are parsing it in string.
  2. In place of jsonObject.getString or .getLong. Use optLong, optString. optString returns the empty string ("") if the key you specify doesn't exist. getString on the other hand throws a JSONException.
Minkoo
  • 439
  • 1
  • 3
  • 16
  • 1
    Wow, awesome! Nice to know that you know how to get String from JSON, but here the question is, how to get only the date part after getting the string from JSON. – Mohammed Atif Mar 15 '17 at 05:07