Reputation: 140
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");
}
Upvotes: 0
Views: 96
Reputation: 590
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");
}
Upvotes: 1