Replace date with “today” or “yesterday”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static String formatToYesterdayOrToday(String date) throws ParseException { Date dateTime = new SimpleDateFormat("hh:mm a dd-MM-yyyy", Locale.getDefault()).parse(date);//EEE hh:mma MMM d, yyyy Calendar calendar = Calendar.getInstance(); calendar.setTime(dateTime); Calendar today = Calendar.getInstance(); Calendar yesterday = Calendar.getInstance(); yesterday.add(Calendar.DATE, -1); SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mma", Locale.getDefault()); if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) { return "Today " + timeFormatter.format(dateTime); } else if (calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR) && calendar.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR)) { return "Yesterday " + timeFormatter.format(dateTime); } else { return date; } } |