javaandroidandroid-recyclerviewandroid-jsondatetime-parsing

How to parse JSON date format inside a RecyclerView onbindViewHolder in android studio?


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


Solution

  • 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;
    }