android 日期 时间
/**
* 给定一个日期型字符串,返回加减n天后的日期型字符串
*
* @param basicDate
* @param nDays
* @return
*/
public static String nDaysAfterOneDateString(String basicDate, int nDays) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Context.getResources().getConfiguration().locale);
Date tmpDate = null;
try {
tmpDate = df.parse(basicDate);
} catch (Exception e) {
// 日期型字符串格式错误
}
long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 - nDays)
* (24 * 60 * 60 * 1000);
tmpDate.setTime(nDay);
return df.format(tmpDate);
}
/**
* 返回今天加减n天后的日期型字符串
*
* @param nDays
* @return
*/
public static String nDaysAftertoday(int nDays) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Context.getResources().getConfiguration().locale);
Calendar rightNow = Calendar.getInstance();
// rightNow.add(Calendar.DAY_OF_MONTH,-1);
rightNow.add(Calendar.DAY_OF_MONTH, -nDays);
return df.format(rightNow.getTime());
}
/**
* 返回今天后几个月的日期型字符串
*
* @param nDays
* @return
*/
public String nDaysAftertoday(int nMonths) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", this
.getResources().getConfiguration().locale);
Calendar rightNow = Calendar.getInstance();
// rightNow.add(Calendar.DAY_OF_MONTH,-1);
rightNow.add(Calendar.MONTH, +nMonths);
return df.format(rightNow.getTime());
}