/* 此函数实现:给定日期和经过天数,算出结果日期
* 其中sDate为指定日期,iDate为多少时间段(可以是 年、月、日... 具体根据iCal来确定)
* iCal为某种时间段例如 月:Calendar.MONTH(具体可查询api中Calendar类)
* sStr为日期格式 例如:"yyyyMMdd"(具体可查询api中Calendar类)
*/
public String getNextDate(String sDate, int iDate,int iCal, String sStr){
String sNextDate = "";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(sStr);
Date date = null;
try {
date = formatter.parse(sDate);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
calendar.add(iCal, iDate);
sNextDate = formatter.format(calendar.getTime());
return sNextDate ;
}
/**
* 返回两个日期之间相差几天
* @param fDate
* @param oDate
* @return
*/
public static int daysOfTwo(Date fDate, Date oDate) {
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(fDate);
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
aCalendar.setTime(oDate);
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
return day2 - day1;
}
public static void main(String[] args) throws ParseException {
Test test = new Test();
String a = test.getNextDate("2014-12-31", 1, Calendar.DATE, "yyyy-MM-dd");
System.out.println(a);
String str1 = "2013-08-11";
String str2 = "2013-08-15";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = simpleDateFormat.parse(str1);
Date d2 = simpleDateFormat.parse(str2);
int day = daysOfTwo(d1, d2);
System.out.println(day);
}