/** * 日期相加减 * @param time * 时间字符串 yyyy-MM-dd HH:mm:ss * @param num * 加的数,-num就是减去 * @return * 减去相应的数量的年的日期 * @throws ParseException */ public static Date yearAddNum(Date time, Integer num) { //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Date date = format.parse(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.add(Calendar.YEAR, num); Date newTime = calendar.getTime(); return newTime; } /** * * @param time * 时间 * @param num * 加的数,-num就是减去 * @return * 减去相应的数量的月份的日期 * @throws ParseException Date */ public static Date monthAddNum(Date time, Integer num){ //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Date date = format.parse(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.add(Calendar.MONTH, num); Date newTime = calendar.getTime(); return newTime; } /** * * @param time * 时间 * @param num * 加的数,-num就是减去 * @return * 减去相应的数量的天的日期 * @throws ParseException Date */ public static Date dayAddNum(Date time, Integer num){ //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Date date = format.parse(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.add(Calendar.DAY_OF_MONTH, num); Date newTime = calendar.getTime(); return newTime; }
java日期加减年月日
猜你喜欢
转载自www.cnblogs.com/renjianjun/p/9182575.html
今日推荐
周排行