How to check if Date parameter has value in Java

I am moving from C # to java and I am having problems with the basics. I expect a variable in my method of type Date, and I need to check if the date was set or not. How to do it in Java? using equal or == ??

Thanks a lot!

+4
source share
2 answers

I suspect you want:

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

Please note that unlike DateTime in .NET, java.util.Date is a class (and therefore a reference type) ... Java does not have β€œcustom” value types.

Also note that this is not exactly the same as the variable being β€œset”. For instance:

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

Do you consider this variable β€œset” or not? It must have a null reference as its value. I suspect you want to know if the variable has a value related to the object, but this is not exactly the same.

+9
source
 if ( myDate != null){ //do your stuff here } 
+4
source

Source: https://habr.com/ru/post/1402278/


All Articles