应用DateUtils实现在指定的时间后分别新增1年、1月、1天、1小时、1分钟、1秒的示例如下:
package timeDemo; import org.apache.commons.lang3.time.DateUtils; import java.text.ParseException; import java.util.Date; public class TImeDemo { public static void main(String[] args) throws ParseException { String dataStr = "2021/07/22 22:21:00"; Date date = DateUtils.parseDate(dataStr, "yyyy/MM/dd HH:mm:ss"); Date newYearDate = DateUtils.addYears(date, 1); System.out.println("newYearDate:" + newYearDate); Date newMonthDate = DateUtils.addMonths(date, 1); System.out.println("newMonthDate:" + newMonthDate); Date newDayDate = DateUtils.addDays(date, 1); System.out.println("newDayDate:" + newDayDate); Date newHourDate = DateUtils.addHours(date, 1); System.out.println("newHourDate:" + newHourDate); Date newMinuteDate = DateUtils.addMinutes(date, 1); System.out.println("newMinuteDate:" + newMinuteDate); Date newSecondDate = DateUtils.addSeconds(date, 1); System.out.println("newSecondDate:" + newSecondDate); } }
执行结果如下:
newYearDate:Fri Jul 22 22:21:00 CST 2022 newMonthDate:Sun Aug 22 22:21:00 CST 2021 newDayDate:Fri Jul 23 22:21:00 CST 2021 newHourDate:Thu Jul 22 23:21:00 CST 2021 newMinuteDate:Thu Jul 22 22:22:00 CST 2021 newSecondDate:Thu Jul 22 22:21:01 CST 2021