Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Null Object pattern with Date?

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.

like image 886
jhyot Avatar asked Jul 24 '26 05:07

jhyot


1 Answers

Optional Class

Try using the Optional class added to Java 8. This article demonstrates.

Guava Library

The Google Guava library has a similar type. This article explains.

like image 130
keuleJ Avatar answered Jul 26 '26 03:07

keuleJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!