Skip to content

Instantly share code, notes, and snippets.

@omegasoft7
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omegasoft7/a51d7aabac183dc65d9f to your computer and use it in GitHub Desktop.
Save omegasoft7/a51d7aabac183dc65d9f to your computer and use it in GitHub Desktop.
public static class DateDiff {
public long seconds;
public long minutes;
public long hours;
public long days;
public DateDiff(long seconds, long minutes, long hours, long days) {
this.seconds = seconds;
this.days = days;
this.minutes = minutes;
this.hours = hours;
}
}
@SuppressWarnings("deprecation")
public static long pastDaysAgo(Date date) {
Date nowDate = new Date(System.currentTimeMillis());
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
nowDate.setHours(0);
nowDate.setMinutes(0);
nowDate.setSeconds(0);
return Math.round((nowDate.getTime() - date.getTime()) / 86400000D);
}
public static DateDiff DiffBetweenDates(Date date1, Date date2) {
long diff = date2.getTime() - date1.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
return new DateDiff(seconds, minutes, hours, days);
}
// Make seconds to date format MM:SS
public static String MakeTime(int time) {
int min = time / 60;
int sec = time - (min * 60);
return (min < 10 ? "0" + min : min) + ":" + (sec < 10 ? "0" + sec : sec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment