I would like to use the Null Object Pattern for java.util.Date. I have a function that returns some Date, but it can also be that no meaningful Date can be returned.
I would like to avoid returning null for the usual reasons why one would like to avoid nulls (nullpointer issues).
At the moment I am returning new Date(0) to indicate an "null date" and checking for returnedDate.equals(new Date(0)) downstream where I need to know whether I have a real date or not, but I'm not convinced that this is the best way.
Optional ClassTry using the Optional class added to Java 8. This article demonstrates.
The Google Guava library has a similar type. This article explains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With