0

how can i display my listview with the following date formats like if its today date show only time , and yesterday ,and tue,mon,sun,sat,fri,thu,wed and agian started with time , ysterday, tue......etc ?

i tried with this code but i'm not getting

if(Constant.hours-Constant.hours1 <= 23){
                    holder.txtTime.setText(newDateString);
                }else if(Constant.hours-Constant.hours1 >= 23 && Constant.hours-Constant.hours1 <= 47){
                    holder.txtTime.setText("Yesterday");
                }else if(Constant.days >= 2 && Constant.days<=2){
                    holder.txtTime.setText(Constant.strLong+" days ago");
                }
McGrady
  • 10,869
  • 13
  • 47
  • 69
Dolly Rosy
  • 140
  • 10

1 Answers1

1

Try this one... //suppose your date time format is "12-08-2017" of variable newDateString then date format will be like SimpleDateFormat formatter = SimpleDateFormat .forPattern("dd-MM-yyyy");//

String newDateString="08/02/2017 18:41:11"; 
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        //SimpleDateFormat formatter = new SimpleDateFormat("your farmat here");
                Date yourDate= null;
                Date current= null;
                Calendar cal = Calendar.getInstance();


     try {
             yourDate= formatter .parse(newDateString);//newDateString  is your variable
                    current= formatter .parse(formatter.format(cal.getTime()));

                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }

                int daysDiff= (int) ((yourDate.getTime() - current.getTime())/ (1000 * 60 * 60 * 24));

                if(daysDiff< 1){
                                holder.txtTime.setText(newDateString);
                            }else if(daysDiff== 1){
                                holder.txtTime.setText("Yesterday");
                            }else if(daysDiff> 1){
                                holder.txtTime.setText(daysDiff+" days ago");
                            }
                            }else if(daysDiff<0){
                                holder.txtTime.setText(daysDiff+" days before");
                            }
Mohd Saquib
  • 580
  • 4
  • 14
  • 08/02/2017 18:41:11 this is my date format from this i need to show results – Dolly Rosy Aug 03 '17 at 12:27
  • String newDateString=08/02/2017 18:41:11; SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); use this you will get your answer with mentioned solution just made this change – Mohd Saquib Aug 03 '17 at 12:28
  • newDateString in this what i need to send? – Dolly Rosy Aug 03 '17 at 12:33
  • newDateString is your date for eg. newDateString="08/02/2017 18:41:11"; BTW i have given full solution according to your answer. According to your mentioned date "08/02/2017 18:41:11" it is giving 175 days before today. – Mohd Saquib Aug 03 '17 at 12:35
  • but according to your mentioned date "08/02/2017 18:41:11" it is giving 175 days before today while i am running this. – Mohd Saquib Aug 03 '17 at 12:55