Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Inserting date in MySQL

 
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 :
 
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
 
Marshal
Posts: 25838
69
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
Marshal
Posts: 25838
69
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....
 
This will take every ounce of my mental strength! All for a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic