I have 2 dates assigned to a Calendar instance. (now, and then).
Calendar currentDate = new Calendar().getInstance();
Calendar oldDate = Calendar.getInstance();
oldDate.setTimeInMillis(plugin.joinTimeLog.getLong(p.getName() + ".time"));
Basically, what I am aiming to achieve is that an event may only occur once per month (every 30 days). If 30 days has not passed, then tell them how long until a month will have passed.
Eg. "21d 17hrs 54mins until you have access."
Here is my current code which seems to give a completely incorrect number of days and hours (note; I have not setup minutes yet).
Calendar timeLeft = Calendar.getInstance();
timeLeft.setTimeInMillis(currentDate.getTimeInMillis() - (oldDate.getTimeInMillis() + (60 * 60 * 24 * 30 * 1000)));
int daysLeft = daysBetween(currentDate, timeLeft);
int hoursLeft = Math.round(hoursBetween(currentDate, timeLeft));
I then use the following code to determine if a month had passed;
if (daysLeft <= 0) {
And here are my methods to obtain the daysLeft and hoursLeft;
public int daysBetween(Calendar d1, Calendar d2) {
return (int)( (d1.getTimeInMillis() - d2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}
public int hoursBetween(Calendar d1, Calendar d2) {
return (int)( (d1.getTimeInMillis() - d2.getTimeInMillis()) / (1000 * 60 * 60));
}