Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

A java date validator

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would break that RE up like this:

 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Got it.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic