给当前时间添加时间
2019-07-15 本文已影响0人
_FireFly_
给当前时间添加年数:
/**
* Adds a number of years to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addYears(final Date date, final int amount) {
return add(date, Calendar.YEAR, amount);
}
给当前时间添加月数:
/**
* Adds a number of months to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addMonths(final Date date, final int amount) {
return add(date, Calendar.MONTH, amount);
}
给当前时间添加周数:
/**
* Adds a number of weeks to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addWeeks(final Date date, final int amount) {
return add(date, Calendar.WEEK_OF_YEAR, amount);
}
给当前时间添加天数:
/**
* Adds a number of days to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addDays(final Date date, final int amount) {
return add(date, Calendar.DAY_OF_MONTH, amount);
}
给当前时间添加小时数:
/**
* Adds a number of hours to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addHours(final Date date, final int amount) {
return add(date, Calendar.HOUR_OF_DAY, amount);
}
给当前时间添加分钟数:
/**
* Adds a number of minutes to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addMinutes(final Date date, final int amount) {
return add(date, Calendar.MINUTE, amount);
}
给当前时间添加秒数:
/**
* Adds a number of seconds to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addSeconds(final Date date, final int amount) {
return add(date, Calendar.SECOND, amount);
}
给当前时间添加毫秒数:
/**
* Adds a number of milliseconds to a date returning a new object.
* The original {@code Date} is unchanged.
*
* @param date the date, not null
* @param amount the amount to add, may be negative
* @return the new {@code Date} with the amount added
* @throws IllegalArgumentException if the date is null
*/
public static Date addMilliseconds(final Date date, final int amount) {
return add(date, Calendar.MILLISECOND, amount);
}
事例:
public static void main(String[] args) {
Date date = new Date();
// Date date1 = DateUtils.addDays(date, 10);
System.out.println("现在的时间是: "+date);
Date date1 = DateUtils.addYears(date, -100);
System.out.println("addYears的时间是: "+date1);
}
控制台: