首页 > 其他分享 >获得指定日期的前几天日期

获得指定日期的前几天日期

时间:2022-10-24 21:32:59浏览次数:62  
标签:specifiedDay String pattern 几天 指定 日期 Calendar before


/**
* 获得指定日期的前几天日期
*
* @param specifiedDay 指定日期
* @param pattern 需要返回的日期格式,如yyyy-MM-dd
* @param before 前几天
* @return String
*/
public static String getSpecifiedDayBefore(String pattern, String specifiedDay, int before) {
Calendar c = Calendar.getInstance();
Date date;
String dayBefore = "";
try {
date = new SimpleDateFormat(pattern).parse(specifiedDay);
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - before);
dayBefore = new SimpleDateFormat(pattern).format(c.getTime());
} catch (Exception e) {
//todo
} finally {
return dayBefore;
}
}


标签:specifiedDay,String,pattern,几天,指定,日期,Calendar,before
From: https://blog.51cto.com/u_15843693/5791309

相关文章