我试图在调用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));