Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Posts
    3

    Unanswered: Best way to inpu Dates from Java application in MySQL

    Hi,

    I'm pretty new to mysql, and a little expirienced in Java and i've come to a problem. I need to put a date that a user inputs in a form in the app in a table field of the type DATE in mysql. I dont need the time, only the Date. The Date format in my area is dd/MM/yyyy. I'm trying something with a JFormattedTextField, although that's new to me too.

    So, i want to know the best way to make that text field (formatted or not) to accept only values in the format mentioned above, and to input that(after Ok is clicked) in a mysql table field without too many lines of code if its possible. I hope this is an easy question for an expirienced programmer. Thanks.

  2. #2
    Join Date
    Mar 2004
    Posts
    480
    have the date put in as yyyymmdd then use date_format to display your results.

  3. #3
    Join Date
    Dec 2004
    Posts
    3
    I don't think I understand what are you saying exactly. I want the users to input the date in the dd/MM/yyyy format. I'm thinking now is there a way to remember that value as some date object and then change the format to what mysql can recognize. Actually is there a way to input values in mysql using some date object or do i have to parse the date value as a String and enter it "manually". And how do i make the text field initialize to the current date, because it's highly unlikely that it will require changes.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    when you provide a date value for mysql, you have several choices for the format of that date value:

    - a string 'YYYY-MM-DD '
    - a string 'YY-MM-DD'
    - a string 'YYYYMMDD'
    - a string 'YYMMDD'
    - a number YYYYMMDD
    - a number YYMMDD

    you cannot insert any other format into a DATE field

    so, to answer your question, you must either

    1) re-assemble the user's input into one of the above formats before you prepare the value to be inserted

    2) insert the user's mm/dd/yyy value into a CHAR or VARCHAR field, and then use SUBSTRING and CONCAT functions in an sql UPDATE statement to set the DATE field to the correct re-assembled value
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Dec 2004
    Posts
    3
    Ok, I get the idea. Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •