/**根據輸入的日期A和天數B,計算出B天后的日期C*/
public static String getLateDate(String sDate,int iDays) {
String sLateDate = "";
Calendar calendar = Calendar.getInstance();
try {
String time = sDate;
String[] arrDate = time.split("-");
int iYear = Integer.valueOf(arrDate[0]);
int iMonth = Integer.valueOf(arrDate[1]);
int iDay = Integer.valueOf(arrDate[2]);
calendar.set(iYear, iMonth, iDay);
calendar.add(Calendar.MONTH, -1);
calendar.add(Calendar.DATE, iDays);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//構造日期格式化器
date = calendar.getTime();
sLateDate = sdf.format(date);
}catch (Exception e) {
LOGGER.error("計算某天數後的日期錯誤",e);
}
return sLateDate;
}
1.使用Calendar應該在set時iMonth-1,而不是add時-1; set時已經指定了時間對象範圍,add只在最後輸出時起作用
DateUtil工具類獲取n天時使用Calendar錯誤
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.