TextView tv_date = (TextView) convertView.findViewById(R.id.tv_date); //getting reference of textview if (tv_date.getVisibility() == View.VISIBLE) { tv_date.setVisibility(View.GONE); } // simplifyDate(DateFormatter.getFormattedDateFromISOString(message.getTimeStamp())) // 2018-06-02T04:27:21.127Z try { if (position > 0) { tempDate = messages.get(position - 1).getTimeStamp().split("T"); chatDate = sdf.parse(String.format(Locale.ENGLISH, "%s", tempDate[0])); } String[] tempDate2 = messages.get(position).getTimeStamp().split("T"); Date chatDate2 = sdf.parse(String.format(Locale.ENGLISH, "%s", tempDate2[0])); int diff = 0; try { if (position == messages.size() - 1 && currentDate.equals(chatDate2) && !chatDate2.equals(chatDate)) { if (tv_date.getVisibility() == View.GONE) { tv_date.setVisibility(View.VISIBLE); } tv_date.setText(getDateString(chatDate2)); } else if (position < messages.size() - 1) { diff = chatDate.compareTo(chatDate2); switch (diff) { case -1: if (tv_date.getVisibility() == View.GONE) { tv_date.setVisibility(View.VISIBLE); } tv_date.setText(getDateString(chatDate2)); break; case 1: case 0: if (tv_date.getVisibility() == View.VISIBLE) { tv_date.setVisibility(View.GONE); } break; } } if (position == 0) { if (tv_date.getVisibility() == View.GONE) { tv_date.setVisibility(View.VISIBLE); } tv_date.setText(getDateString(chatDate2)); } } catch (Exception e) { Log.e(TAG, "Date Compare: Exception: " + e.getMessage()); } } catch (ParseException e) { Log.e(TAG, "getView: ParseException: " + e.getMessage()); } catch (Exception e) { Log.e(TAG, "getView: Exception: " + e.getMessage()); } @NonNull private String getDateString(@NonNull Date chatDate2) { String s = null; if (today.equalsIgnoreCase(sdf.format(chatDate2))) { // if its toady date s = activity.getString(R.string.date_today); } else if (yesterday.equalsIgnoreCase(sdf.format(chatDate2))) { // else if its yesterday date s = activity.getString(R.string.date_yesterday); } else { // else print date in WhatsApp format "Wed, 21 May" s = new SimpleDateFormat("EEE, dd MMM", Locale.getDefault()).format(chatDate2); } return s; } private String getYesterdayDate() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return sdf.format(cal.getTime()); }
