0

I have a JSONObject and an ArrayList from where i get the data, the data date string i get is yyyy/MM/dd 00:00:00, i only want to show the day of the month 1-31, how would i format that string to only show the day ?

I've tried to use SimpleDateFormat, but without any luck, maybe since I'm doing this inside a for loop ?

public void onResponse(String response) {

                        try {
                            //getting the whole json object from the response
                            JSONObject obj = new JSONObject(response);

                            ArrayList<ListModel> ListModelArrayList = new ArrayList<>();
                            JSONArray dataArray = obj.getJSONArray("events");

                            for (int i = 0; i < dataArray.length(); i++) {

                                ListModel List = new ListModel();
                                JSONObject dataobj = dataArray.getJSONObject(i);

                                List.setId(dataobj.getString("id"));
                                List.setInit_date(dataobj.getString("init_date"));
                                List.setEnd_date(dataobj.getString("end_date"));
                                List.setTitle(dataobj.getString("title"));
                                List.setDescription(dataobj.getString("description"));
                                List.setColor_code(dataobj.getString("color_code"));
                                List.setAll_day(dataobj.getString("all_day"));

                                ListModelArrayList.add(List);
                            }

                            for (int j = 0; j < ListModelArrayList.size(); j++) {

                                textViewDate.setText(textViewDate.getText() +
                                        ListModelArrayList.get(j).getInit_Date() + "\n");


                                textViewEvent.setText(textViewEvent.getText() +
                                        ListModelArrayList.get(j).getTitle() + "\n");

                            }

right now i am getting this format 2019-05-17 00:00:00, i want to display 17 only

Nancy
  • 1,021
  • 4
  • 23
  • 45
  • check this answer https://stackoverflow.com/a/17192941/7586266 – Hossam Eldeen Onsy Jul 06 '19 at 18:44
  • Since your question is tagged simpledateformat: consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 08 '19 at 09:41

1 Answers1

0

You can alter SimpleDateFormat as you suggested and instead of putting the date directly in the for loop here :

textViewDate.setText(textViewDate.getText() +
                                    ListModelArrayList.get(j).getInit_Date() + "\n");

You will do the following inside your loop :

String s = ListModelArrayList.get(j).getInit_Date();
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date date = null;
    try {
        date = fmt.parse(s);
        SimpleDateFormat fmtOut = new SimpleDateFormat("dd", Locale.ENGLISH);

        textViewDate.setText(textViewDate.getText() +
                fmtOut.format(date) + "\n");
    } catch (ParseException e) {
        textViewDate.setText(textViewDate.getText() + "\n");
        e.printStackTrace();
    }

Which allows you to format the date according to the pattern you wish in this line :

 SimpleDateFormat fmtOut = new SimpleDateFormat("dd", Locale.ENGLISH);

references for patterns Oracle Documentation SimpleDateFormat Patterns

  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jul 08 '19 at 08:36