2

I'm trying to use a variable for the number of days in my call to DateUtil.addDays, but it's not working. Am I missing something simple, or does it just not work?

//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.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Welcome to StackOverflow. Keep in mind that people cannot help you find the correct solution if they can't see all the relevant details of your problem. For instance : What is the type of variable `x` ? What is the "error" : compilation ? run-time exception? What is the error message ? – Pac0 Aug 31 '17 at 23:06
  • In general, using a value as function argument `foo(10);` or a variable `int x = 10; foo(x);`will be treated similarly by the Java language, but you have to be careful about which **types** are required in your function, and be aware that some automatic type conversion can happen too. Here, the `addDays` method requires an `int`as second argument. So, is your `x` variable an `int`? – Pac0 Aug 31 '17 at 23:09
  • Thanks for the comment! I tested x with the answer for this question: https://stackoverflow.com/questions/12558206/how-can-i-check-if-a-value-is-of-type-integer and the result came back as int – Herecomedatby Aug 31 '17 at 23:30
  • Be careful ! The test you linked is to check if the **value is an integer**, not if the **type of the variable is an integer** ! example : `float x = 2.0; ` will pass your test, the value is the same as the integer 2, but the variable is **not** an `int`, it's a `float`, and any attempt to use this variable in a function asking for an `int` will cause a compilation error ! – Pac0 Aug 31 '17 at 23:34
  • 1
    You should search your code and check the **declared type of x**. – Pac0 Aug 31 '17 at 23:35
  • If it is a float / double or so, you can try to get an `int`out of it by, for instance, use the `Math.round(x)` function. But to tell you the correct answer, I have to know the type of variable `x` : double / float / string / other... – Pac0 Aug 31 '17 at 23:38
  • Well now don't I feel silly. I simply converted via "int xyz = x" and now it works. Thank you very much Mr. Pac0! :) – Herecomedatby Aug 31 '17 at 23:54
  • 1
    You are welcome. This is a usual mistake to do when learning Java and other "strongly typed languages". – Pac0 Aug 31 '17 at 23:56

1 Answers1

0

You have to be careful with your types in Java.

Examples :

// 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));
Pac0
  • 21,465
  • 8
  • 65
  • 74