Skip to content

Instantly share code, notes, and snippets.

@suleymanbilgin
Last active December 30, 2016 13:21
Show Gist options
  • Save suleymanbilgin/acd33eaee77e32606296ea13346457ae to your computer and use it in GitHub Desktop.
Save suleymanbilgin/acd33eaee77e32606296ea13346457ae to your computer and use it in GitHub Desktop.
/**
* @param realDate your real date -> dd/MM/yyyy
* */
public static String controlDateWithTimestamps(String realDate) {
try {
Long currentTimestamp = System.currentTimeMillis() / 1000;
DateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
Date messageDate = (Date) formatter2.parse(realDate);
Long messageTimestamp = messageDate.getTime() / 1000;
Long z = currentTimestamp - messageTimestamp;
if (z <= (1 * 24 * 60 * 60)) {
// today
return "today";
} else if (z <= (2 * 24 * 60 * 60) && z > (1 * 24 * 60 * 60)) {
// yesterday
return "yesterday";
} else if (z <= (7 * 24 * 60 * 60) && z > (2 * 24 * 60 * 60)) {
// inside last 7 day
String weekDay;
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE");
weekDay = dayFormat.format(messageTimestamp*1000);
return weekDay;
} else {
// a week ago
return realDate;
}
} catch (Exception e) {
return realDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment