public class Time {
public static int getTimeInterval(Date beforeTime, Date afterTime){
Calendar cal = Calendar.getInstance();
cal.setTime(beforeTime);
long time1 = cal.getTimeInMillis();
cal.setTime(afterTime);
long time2 = cal.getTimeInMillis();
long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
calendar.roll(Calendar.DATE, -1);
return calendar.getTime();
}
public static int getTimeInterval(String beforeTime, String afterTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int days = 0;
try {
Date date1 = format.parse(beforeTime);
Date date2 = format.parse(afterTime);
days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
public static int getDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}