我想在一个特定的日期上加上一天。我该怎么做呢?

Date dt = new Date();

现在我想在这个日期上加一天。


当前回答

我发现了一个简单的方法,可以向Date对象添加任意时间

Date d = new Date(new Date().getTime() + 86400000)

地点:

86 400 000 ms = 1 Day  : 24*60*60*1000
 3 600 000 ms = 1 Hour :    60*60*1000

其他回答

您可以在导入org.apache.commons.lang.time.DateUtils后使用此方法:

DateUtils.addDays(new Date(), 1);

在非常特殊的情况下,如果你要求做自己的约会课,可能是你的计算机编程教授;这个方法会做得很好。

public void addOneDay(){
    int [] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    day++;
    if (day> months[month-1]){
        month++;
        day = 1;
        if (month > 12){
            year++;
            month = 1;
        }
    }
}

你可以像这样尝试java.util.Date库-

int no_of_day_to_add = 1;

Date today = new Date();
Date tomorrow = new Date( today.getYear(), today.getMonth(), today.getDate() + no_of_day_to_add );

根据需要更改no_of_day_to_add的值。

我设置了no_of_day_to_add的值为1,因为你只想添加一天。

更多信息可以在本文档中找到。

Java 8 LocalDate API

LocalDate.now().plusDays(1L);

我将向您展示如何在Java 8中做到这一点。给你:

public class DemoDate {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);

        //add 1 day to the current date
        LocalDate date1Day = today.plus(1, ChronoUnit.DAYS);
        System.out.println("Date After 1 day : " + date1Day);
    }
}

输出:

Current date: 2016-08-15
Date After 1 day : 2016-08-16