Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting date in MySQL

 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
I try to insert some dates in MySQL from the Java code but i fail to insert it in proper format. The default format of MySQL is "yyyy-MM-dd". So i set this as my default too. But when i insert the date it changes like this.. "0029-22-08"..Where '08 is the year field... Why this happens.. How could i change that to its proper default format... And here is my code


: Edited :
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rajkumar,

this is what I understand from your problem.

Your inputdate is a String, in the dd-MM-yyyy format, and you want to create a Date object, right?

Here's an example:


hope this helps,
Herman
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have some code that does nothing, then you have some code which uses Strings that aren't in the right format for java.sql.Date. Here's better code:
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How that'd be false... I just parse the date and store it in d1 and then format it to the required format and transfer it to date variable. Is this wrong? Just for clarification only i ask this!!
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajkumar balakrishnan:
How that'd be false... I just parse the date and store it in d1 and then format it to the required format and transfer it to date variable. Is this wrong? Just for clarification only i ask this!!
You parsed the date string and then formatted it to the format it was originally stored in. Now if you had formatted it to the required format -- which you didn't -- that would have worked. It would have been clumsy but it would have worked.

Note that your preferred way is like this:

String -> java.util.Date -> String -> java.sql.Date

Here there are three conversions between Strings and internal formats. My preferred way is like this:

String -> java.util.Date -> java.sql.Date

Here there is one conversion betwen Strings and internal formats. And also, you don't have to know which date format java.sql.Date's constructor requires.
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for clarifying my doubt and probably i might get some good result with the code...
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

i got a clear definition with my problem regarding storing date in mysql... and i now retrieve the date using Preparedstatement's getDate() method but it shows date by a default format like 'yyyy/MM/dd'.. But i want it to display in 'dd/MM/yyyy' format... How could i achieve it..Please guide me....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic