0

I have looked everywhere but I couldn't find anything about parsing a date time json object inside RecyclerView onbindViewHolder in android.

How to do it inside a recycler view adapter?

I’m new in Rest Api with RecyclerView, so if you know the solution please help, thanks.

public class AdminAdvanceRefundRequestAdapter extends RecyclerView.Adapter<AdminAdvanceRefundRequestAdapter.MyViewHolder> {

        List<AdvanceReturnRequestModel> advanceReturnRequestModels;
        private Context context;
        private RecyclerViewClickListener mListener;

        public AdminAdvanceRefundRequestAdapter(List<AdvanceReturnRequestModel> advanceReturnRequestModels, Context context, RecyclerViewClickListener mListener) {
            this.advanceReturnRequestModels = advanceReturnRequestModels;
            this.context = context;
            this.mListener = mListener;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_advance_refund_request, parent, false);
            return new MyViewHolder(view, mListener);
        }

        private String getFormate(String date) throws ParseException {
            Date d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse(date);
            //Log.d("Date", String.valueOf(d));
            Calendar cal = Calendar.getInstance();
            cal.setTime(d);
            String monthName = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").format(cal.getTime());
            return monthName;
        }

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

            final AdvanceReturnRequestModel thisModelResponse = advanceReturnRequestModels.get(position);       

            holder.return_date.setText(getFormate(thisModelResponse.getCreatedDate()));

        }

        @Override
        public int getItemCount() {
            return advanceReturnRequestModels.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder{

            private RecyclerViewClickListener mListener;
            private TextView return_date;        
            private RelativeLayout row_container_arr;

            public MyViewHolder(@NonNull View itemView, RecyclerViewClickListener listener) {
                super(itemView);

                return_date = itemView.findViewById(R.id.patient_returnDate_ARR_Model);            

                mListener = listener;

            }
        }

        public interface RecyclerViewClickListener {
            void onRowClick(View view, int position);
            void onButtonYesClick(View view, int position);
            void onButtonNoClick(View view, int position);
        }
    }

My JSON format is this:"CreatedDate": "/Date(1575781410847)/", and have to show it like this: 08-12-2019 11:03 AM

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Can you clearly mention your problem and what do you actually want? Also post what your `String date` looks like that you want to format? – Md. Asaduzzaman Jan 19 '20 at 06:28
  • My JSON format is this:"CreatedDate": "/Date(1575781410847)/", and have to show it like this: 08-12-2019 11:03 AM – Aleef Faminn Jan 19 '20 at 06:33
  • That means `thisModelResponse.getCreatedDate()` return `/Date(1575781410847)/`. Am I right? – Md. Asaduzzaman Jan 19 '20 at 06:38
  • Yes Sir, you are right – Aleef Faminn Jan 19 '20 at 06:41
  • I don't know why your server return date like this format. If you have access to change the format then simply return like this `1575781410847`, which is easy to parse – Md. Asaduzzaman Jan 19 '20 at 06:49
  • Sorry, I don't have back end access – Aleef Faminn Jan 19 '20 at 06:51
  • 1
    Does this answer your question? [Convert Json date to java date](https://stackoverflow.com/questions/24956396/convert-json-date-to-java-date) I am immodest enough to recommend [my own answer there](https://stackoverflow.com/a/57985871/5772882), but there are others too. Can you use my answer using java.time on Android? Yes you can. If for Android under API level 26, then through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Jan 19 '20 at 18:21
  • It probably doesn’t turn up as the first hit when you search, but it seems to me that you only needed to search a little more thoroughly, maybe try a couple of more searches, maybe look through the first 5 or 10 search results. – Ole V.V. Jan 19 '20 at 18:26
  • Thank you sir, I'll remember that. – Aleef Faminn Jan 22 '20 at 04:28

1 Answers1

0

Actually your provided date string is not parsable. You have to get formattable date string from your CreatedDate. Check below:

private String getFormate(String date) {
    String formattedDate = null;

    try {
        String[] dates = date.split("\\(|\\)");
        Long timeInMillis = Long.parseLong(dates[1]);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
        formattedDate = simpleDateFormat.format(calendar.getTime());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return formattedDate;
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46