日期:2014-05-20  浏览次数:20600 次

Date 类型 加减问题
private Date startTime;// 开通时间

private Date endTime;// 结束时间

private Date remain;//剩余天数

怎么减得到remain
代码应该怎么写
------解决方案--------------------
加减时间的话用Calender!

/**
 * 得到几天前的时间
 * 
 * @param d
 * @param day
 * @return
 */
public static Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}

/**
 * 得到几天后的时间
 * 
 * @param d
 * @param day
 * @return
 */
public static Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}

------解决方案--------------------
DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
int x=new Long((format.parse(t1).getTime()-format.parse(t2).getTime())/(1000 * 60 * 60 * 24)).intValue();