在java中,现在将日期增加几天

在java中,现在将日期增加几天,java,Java,我需要把日期增加几天 private Date now = new Date(); private Date result; public void incrementDate(Integer days) { result = } 因此,如果天数等于3,我需要在3天内增加我的now date,并将其设置为result 我知道Java8在LocalDate类中有plusDays方法。有没有办法在Java7中实现这一点。使用 加上其他有趣的东西。用于: Calendar cal =

我需要把日期增加几天

private Date now = new Date();
private Date result;

public void incrementDate(Integer days) {

    result = 

}
因此,如果天数等于3,我需要在3天内增加我的
now date
,并将其设置为
result

我知道Java8在
LocalDate
类中有
plusDays
方法。有没有办法在Java7中实现这一点。

使用

加上其他有趣的东西。

用于:

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE,3);
result = cal.getTime()

我建议您将函数设为静态,然后立即传入
。返回
日期
并使用
日历
。大概

public static Date incrementDate(Date now, int days) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(now);
    cal.add(Calendar.DAY_OF_MONTH, days);
    return cal.getTime();
}
然后测试它

public static void main(String[] args) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date now = new Date();
    System.out.println(df.format(now));
    System.out.println(df.format(incrementDate(now, 3)));
}
这里(今天)的输出是

请尝试以下代码:

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar cal=Calendar.getInstance();
String today=sdf.format(cal.getTime());
System.out.println(today);
cal.add(Calendar.DATE, 20);
String After=sdf.format(cal.getTime());
System.out.println(After);
Date now = new Date();

Calendar
JodaTime
2014-11-12
2014-11-15
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar cal=Calendar.getInstance();
String today=sdf.format(cal.getTime());
System.out.println(today);
cal.add(Calendar.DATE, 20);
String After=sdf.format(cal.getTime());
System.out.println(After);
Date now = new Date();