0

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.

tst
  • 13
  • 2
musica
  • 1,373
  • 3
  • 15
  • 34

1 Answers1

1

The timestamp defines a specific instant - a point in the timeline.

The same instant can correspond to a different date and time in different timezones.

Example: right now, at this moment, System.currentTimeMillis() returns a timestamp value of 1519050435812. This same instant represents:

  • February 19th 2017, 11:27 AM in São Paulo
  • February 19th 2017, 2:27 PM in London
  • February 20th 2017, 3:27 AM in Auckland

Concepts like "today" and "current time" are relative, because they change according to the place you are (the timezone/region/city/country/whatever you are refering to).

A timestamp is an absolute value, because it's the same everywhere in the world.

To translate the timestamp (an absolute value) to a date and time (a relative concept), you must choose a timezone to work with. Changing the timezone will always change the final result - actually, when you change the timezone, it's expected to have a different result.

tst
  • 13
  • 2
  • That's why i want to consider device's local timezone while formatting. I don't want to specify any particular timezone in my logic instead it should take timezone of user's device. – musica Feb 20 '18 at 04:43