Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Date parameter has some value in Java

I'm moving from C# to java and I'm having troubles with the basics. I'm expecting a variable in my method of type Date and I need to check if the Date has been set or not. How do I do that in Java? using ,equals or == ??

Thanks a lot!

like image 494
Dragan Avatar asked Dec 12 '22 04:12

Dragan


1 Answers

I suspect you want:

public void foo(Date date) {
  if (date != null) {
    // Use date here
  }
}

Note that unlike DateTime in .NET, java.util.Date is a class (and therefore a reference type)... Java doesn't have "custom" value types.

Also note that that's not quite the same as whether or not the variable has been "set". For example:

Date date = new Date();
date = null;

Would you consider that variable to be "set" or not? It's set to have a null reference as its value. I suspect you want to know whether the variable has a value referring to an object, but that's not quite the same thing.

like image 116
Jon Skeet Avatar answered Dec 26 '22 20:12

Jon Skeet