I love this place!
Something like this?Declan Dee wrote:I need to change so that if an object is created of type date and this date is before a certain date e.g. 12/01/1977 that a message is generated.
That's not what you said - you said you wanted a message displayed if the requested date was before a certain threshold date. If you want to stop construction if the requested date is before the threshold, you should throw an exception:So basically what I am saying is that I want to use the constructor to control what object can be created.
For this, you just need to compare with the high and low dates (using calendars):But if you're going to use the Calendar class, why not use the SDK Date class (or better still, the JodaTime classes) ? Why reinvent the wheel?The second thing that I am looking for is to make sure that a date entered must be between two specific dates and not outside
Campbell Ritchie wrote:Why are you trying to control creation from inside the constructor? Is it because you have a class invariant that the values must be in a particular range? If so, I think the IllegalArgumentException technique looks better to me.
BEE MBA PMP SCJP-6
Jim Hoglund wrote:How about making your constructor private and using MyDateClass.getInstance(day, month, year)
to police the dates. It can return null if the date is out of range.
BEE MBA PMP SCJP-6
Jim Hoglund wrote:Okay, but does it not depend if an out-of-range date is truly an
error, or just something you want to screen out?
Paul Clapham wrote:Let's suppose you implement this business rule ("The hire date can't be before the date the company was formed") by having this Date object throw an exception. That's certainly valid Java code, and it might be the right thing to do in some contexts.
So yes, you can certainly implement your constructor as you originally asked. From a technical point of view, that is. But there are a lot of situations where you shouldn't implement it that way, instead having something else validate the data before calling the constructor.
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
|