DateUtil.AddDays 是否接受天数的变量?

Does DateUtil.AddDays accept a variable for the # of days?

我试图在调用 DateUtil.addDays 时使用天数变量,但它不起作用。我是否遗漏了一些简单的东西,或者它只是不起作用?

//Example:
x = 10  //this int is the result of some math, and changes frequently
y = Thu Aug 31 00:00:00 MST 2017  //this is the date

z = DateUtil.addDays(y, x)   //This will error.
z = DateUtil.addDays(y, 10)  //This works.

您必须小心 Java 中的类型。

示例:

// suppose variable y contains your date as in your example
int x = 10;                   // value is 10
double w = x;                 // value is same as 10 (or, more precisely, 10.0)

z = DateUtil.addDays(y, x);   // This will compile. 

// This will not compile. AddDays expect an argument of type `int`,
// not a `double`, even if the value inside is mathematically
// the same as the int 10.
 z = DateUtil.addDays(y, w);

// This will compile : the result of Math.round() is of type int
z = DateUtil.addDays(y, Math.round(w));