In my app, I want to check if the received epoch time from backend is today's date or yesterday's date or previous day of the week to show day accordinly. If it is doesn't match any of these condition then I want to simply show the date.
Here my logic to check these conditions,
public static String pastTime(final Long epochTime) {
if (TimeCalc.isToday(epochTime)) {
return TimeCalc.getTime(epochTime);
} else if (TimeCalc.isYesterday(epochTime)) {
return YESTERDAY;
} else if (TimeCalc.isPreviousWeek(epochTime)) {
return TimeCalc.getDay(epochTime);
} else {
return generateDate(epochTime);
}
}
to check if today:
public static boolean isToday(Long timestamp) {
Long todayMillis = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat(Timestamp.ISO_8601_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
String time = formatter.format(calendar.getTime());
calendar.setTimeInMillis(todayMillis);
String today = formatter.format(calendar.getTime());
return time.equals(today);
}
to check isYesterday:
public static boolean isYesterday(Long timestamp) {
Long millisInADay = TimeUnit.DAYS.toMillis(1);
Long todayMillis = System.currentTimeMillis();
Long daysToday = todayMillis / millisInADay;
Long daysTime = timestamp / millisInADay;
if (daysTime == daysToday - 1) {
return true;
} else {
return false;
}
}
to check previous day of the week:
public static boolean isPreviousWeek(Long timestamp) {
Long millisInADay = TimeUnit.DAYS.toMillis(1);
Long todayMillis = System.currentTimeMillis();
Long daysToday = todayMillis / millisInADay;
Long daysTime = timestamp / millisInADay;
if (daysToday - daysTime <= 6) {
return true;
} else {
return false;
}
}
The problem is when I check on different timezone it is not giving me expected results.