• Post Reply Bookmark Topic Watch Topic
  • New Topic

A java date validator  RSS feed

 
Thomas Nilsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new to java, and is just building small programs to get the feel for the language. Any tips on how I can improve this code would be greatly appreciated.


 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would break that RE up like this:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your choice of using an enum for this seems a little strange to me. It doesn't fit my mental model of an enum.
 
Thomas Nilsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Your choice of using an enum for this seems a little strange to me. It doesn't fit my mental model of an enum.


I actually worked "backwards".

I started out by asking myself - How do I want to call the validation method, and set the date format at the same time? Enum seemed like the most logical choice to me.
You could do so much more with enum in java as opposed to other languages.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what happens when someone wants to validate a date that's not formatted in a way that you have defined in one of these enums? Seems like this design violates the Open-Closed Principle
 
Thomas Nilsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:But what happens when someone wants to validate a date that's not formatted in a way that you have defined in one of these enums? Seems like this design violates the Open-Closed Principle



Do you have any specific date formats in mind?

Right now it accepts:
DDMMYYYY
MMDDYYYY
YYYYMMDD

DD.MM.YYYY
MM.DD.YYYY
YYYY.MM.DD

DD-MM-YYYY
MM-DD-YYYY
YYYY-MM-DD

DD/MM/YYYY
MM/DD/YYYY
YYYY/MM/DD

Only thing I can think would be if one would set a 2 digit year (YY)?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it will validate mmddyy with or without separators. This is because the constructor always assumes day, month, year.
 
Thomas Nilsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I don't think it will validate mmddyy with or without separators. This is because the constructor always assumes day, month, year.


Not mmddyy, because I assume year is 4 digits. see my answer above for what date version it accepts right now..
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Got it.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Nilsen wrote:
Do you have any specific date formats in mind?

That's beside the point. If this is meant for limited use within an application or within your company where you have a limited set of date formats to support, then I suppose it's OK. If you need to make it available to a wider audience or make it more flexible over time, then the enum doesn't work as well.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any specific date formats in mind?

How about 2 Mar 15 or 1/4/15 or dd-mm-(yy)yy (traditional Danish) or yyyy年m月d日 (Chinese)
 
Thomas Nilsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Do you have any specific date formats in mind?

How about 2 Mar 15 or 1/4/15 or dd-mm-(yy)yy (traditional Danish) or yyyy年m月d日 (Chinese)


Months as actual text which cannot be parsed to ints is a problem...

dd-mm-yyyy will work, but not with dd-mm-yy.

But, when you write year with only yy, how will one know if i mean 2020 or 1920 for example?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Nilsen wrote:But, when you write year with only yy, how will one know if i mean 2020 or 1920 for example?

The simple answer is you don't know. Generally software that accepts 2 digit years and which needs to know the century part has a cut off year and converts the 2 digit year to 4 digits along the lines of the following: years below the cut off are assumed to be 20xx and years from the cut off onwards are assumed to be 19xx.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!