I have a class with the fields "deletionDate" and "experiationDate" which could both be undefined, what would mean that the object is whether deleted nor has an expiration date.
My first approach was:
private Date deletionDate = null; // null means not deleted
Having the book "Clean Code" in mind I remember to better use expressive names instead of comments. So my current solutions is:
private static final Date NEVER = null;
private Date deletionDate = NEVER;
I could user a wrapper class around Date, but that would complicate JPA mapping.
What do you think of it? How would you express "never"?
For the legacy java. util. Date , uses new Date() or new Date(System. currentTimeMillis() to get the current date time, and format it with the SimpleDateFormat .
The java. util. Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time). The class is used to keep coordinated universal time (UTC).
You can't set a Date to null . Date is an an embedded JavaScript type. Setting date to new Date(0) just sets it to Unix Epoch. You can, however, set a variable to null .
I use the following SimpleDateFormat to parse a string, SimpleDateFormat ft= new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); String notimeZone = ft. format(startDateTime); Date date = ft. parse(notimeZone);
well never is never, not the 1/1/2999.
I would stay with your 1st solution. a Null date means it has not yet happened.
maybe you can wrap it with something like :
boolean isNeverDeleted(){
return deletionDate == null;
}
You can think about null date as "not available" or "not applicable". If that's the case "NO DATE" is fine for "never".
Don't subtype Date only for a very exquisite style requirement.
A better option is to add semantic to your model object. If your have a Thing object with a deletionDate property you can do:
class Thing
+ deletionDate
+ isNeverDeleted: boolean { return deletionDate == null; }
and it will be practical and documentative, both in the class and in your client code:
if(myThing.isNeverDeleted())
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