两日期间隔

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.sql.Date;

public class DateUtil{

/**
* 返回两日期距离天数
*/
public static Long getDaysBetween(Date startDate, Date endDate) {     
        Calendar fromCalendar = Calendar.getInstance();     
        fromCalendar.setTime(startDate);     
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);     
        fromCalendar.set(Calendar.MINUTE, 0);     
        fromCalendar.set(Calendar.SECOND, 0);     
        fromCalendar.set(Calendar.MILLISECOND, 0);     
     
        Calendar toCalendar = Calendar.getInstance();     
        toCalendar.setTime(endDate);     
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);     
        toCalendar.set(Calendar.MINUTE, 0);     
        toCalendar.set(Calendar.SECOND, 0);     
        toCalendar.set(Calendar.MILLISECOND, 0);     
     
        return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);     
    }

/**
* 计算 interval 天后的时间日期
* @return
*/
public static Date getDateByInterval(Date date,int interval){

Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, +interval);//取当前日期的后一天.
return cal.getTime();

}
  
}

你可能感兴趣的:(日期)