Skip to content

Instantly share code, notes, and snippets.

@usbportnoy
Last active September 29, 2016 03:34
Show Gist options
  • Save usbportnoy/457fdc9c64a45d2b8578637f82c3ef52 to your computer and use it in GitHub Desktop.
Save usbportnoy/457fdc9c64a45d2b8578637f82c3ef52 to your computer and use it in GitHub Desktop.
public class OnlineStatus {
private int recentOnlineThresholdDays;
private int onlineThresholdHrs;
public enum States {Unknown, Online, Recent}
public void setOnlineThresholdHrs(int hours) {
this.onlineThresholdHrs = hours;
}
public void setRecentOnlineThresholdDays(int days) {
this.recentOnlineThresholdDays = days;
}
public States getStatus(Date lastOnline, Calendar calendar) {
if (onlineThresholdHrs <= 0) {
throw new IllegalArgumentException("Requires online threshold in hours");
}
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
int currentDay = calendar.get(Calendar.DAY_OF_YEAR);
States state = States.Unknown;
calendar = Calendar.getInstance();
calendar.set(
Calendar.DAY_OF_YEAR,
currentDay - recentOnlineThresholdDays
);
Date recentOnlineThreshold = calendar.getTime();
if (lastOnline.after(recentOnlineThreshold)) {
state = States.Recent;
}
calendar = Calendar.getInstance();
calendar.set(
Calendar.HOUR_OF_DAY,
currentHour - onlineThresholdHrs);
Date onlineThreshold = calendar.getTime();
if (lastOnline.after(onlineThreshold)) {
state = States.Online;
}
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment