Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express "never" with java.util.Date?

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"?

like image 920
deamon Avatar asked Jan 08 '10 11:01

deamon


People also ask

How do I get Java Util date?

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 .

How does Java Util date work?

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).

Can we set date as null in Java?

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 .

How do I remove timezone from Java Util date?

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);


2 Answers

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;
}
like image 194
chburd Avatar answered Oct 04 '22 22:10

chburd


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())
like image 26
helios Avatar answered Oct 04 '22 22:10

helios