如何在 android 工作室的 RecyclerView onbindViewHolder 中解析 JSON 日期格式?

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

我找遍了所有地方,但找不到任何关于在 android.

中的 RecyclerView onbindViewHolder 中解析日期时间 json 对象的信息

如何在回收器视图适配器中执行此操作?

我是 RecyclerView 的 Rest Api 新手,所以如果您知道解决方案请帮忙,谢谢。

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

实际上您提供的日期字符串无法解析。您必须从 CreatedDate 中获取可格式化的日期字符串。检查以下内容:

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