JAVA日期计算

来源:百度知道 编辑:UC知道 时间:2024/06/23 23:45:59
如何计算 2001-01-01 后的第10天是 2001-01-11?
写下算法

import java.util.Date;

public class DateUtil
{
/**
* milliseconds in one day
*/
public static final long MILLIS_IN_DAY = 1000 * 60 * 60 * 24;

/**
* get the n th date after the given date
*/
public static Date getNextNDate(Date date, int n) {
return new Date(date.getTime() + DateUtil.MILLIS_IN_DAY * n);
}

public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
System.out.println("ten days later");
System.out.println(getNextNDate(now,10));
}
}