Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date used as String datatype

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problem i can face if i store date as STRING (in java & varchar2 in database)
 
Martin Vajsar
Sheriff
Pie
Posts: 3751
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say:

  • You won't be able to perform date-time arithmetics easily.
  • You'll be able to store strings that do not represent a valid date (with dates, you'd get an exception instead, which would help you to pinpoint the origin of the error).
  • Performance will suffer in some databases, as cardinality estimates for query plan optimization will be much worse than with proper data types.
  • Unless you're really careful, you'll have implicit conversions in SQL, which are evil and can expose you to some arcane forms of SQL injection atacks.

  • Unless you never manipulate the dates in other ways than displaying them, I'd strongly discourage this. If you ever need to add/subtract time, convert a date to local timezone or even search a range of dates in the database, use proper data types.
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your reply Martin Vajsar.

    What do you say about date as String datatype in java code and Date datatype in database?
     
    Martin Vajsar
    Sheriff
    Pie
    Posts: 3751
    62
    Chrome Netbeans IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Slightly better then the previous option, in my opinion, but I still wouldn't do it.

    JDK unfortunately lacks good date/time support. I'd suggest to check the JodaTime library, it makes handling the dates in Java a much pleasant experience.
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Slightly better then the previous option, in my opinion, but I still wouldn't do it.


    What are your reasons? I am using it in a project, so, your reasons will help me to decide much better
     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20769
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's turn it around. What is your compelling reason to store a date as a String object in your code? You're the one who has to justify this. Nobody has to provide reasons for storing a date in a Date object.
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul, you got me.

    Frankly speaking i found it( store a date as a String object) simple and easy to use. But, i still wonder, what problems it can cause.


     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20769
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's a typical beginner idea to store everything as strings. Eventually they get to the chapter in their book which tells them how to design and create classes and objects, and then they don't do that any more.

    There's no point in going through the possible things which can go wrong if you don't use proper object-oriented design in an object-oriented language, at least not in my opinion.
     
    Martin Vajsar
    Sheriff
    Pie
    Posts: 3751
    62
    Chrome Netbeans IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kunal Lakhani wrote:
    Slightly better then the previous option, in my opinion, but I still wouldn't do it.

    What are your reasons? I am using it in a project, so, your reasons will help me to decide much better

    Why is it a bit better? Because the four points I listed earlier won't apply at the database side. So if you get an invalid date string and try to store it, it will fail and you won't get a nonsensical date value in the database. Big plus to me.

    Why only slightly better? Because most of these four points would still apply on the Java side.

    If you try JodaTime, you'll find you handling dates can be very easy and simple in Java. In my opinion, if your application is date-time intensive, it is a must. Heck, even if all you need is to add one day to a date in your application, my advice is: use JodaTime. But I promise I'll stop plugging it in this thread from now on.
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Paul Clapham and Martin Vajsar.

    Martin Vajsar, i will try my best to dive in JodaTime
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic