• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Converting MySQL Datetime to MS SQL Datetime and vice versa !

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

I want to convert MySQL datetime(yyyy-mm-dd hh:mm:ss) to MS SQL datetime(mm-dd-yyyy hh:mm:ss) . I googled it hardly but no success .

Please help me !

Thanks in advance !
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about two SimpleDateFormat objects? You use the first to convert the MySQL date string into a Date object, then use the second to convert that Date object back into a string.

However, if you retrieve the values directly from the database, you shouldn't use Strings at all. ResultSet and PreparedStatement can work with Date objects: java.sql.Date for the date only, java.sql.Time for the time only, and java.sql.Timestamp for both date and time. All three classes extend java.util.Date. Conversion between them can be done with the constructors:
 
Fidel Edwards
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:



Thanks Rob ! For Early reply !

Meanwhile I also tried a new thing as



During insertion I used



But here it is not working as showing



 
Rob Spoor
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not all database systems handle dates the same. Some may use a full long, while others may only use half of that. That's why you should use the date/time types in the java.sql packages:
Don't mix the types; use getTimestamp and setTimestamp for both date/time, not getDate. That may remove the time information. For a column called insert_time that's probably not what you want.

You may want to read http://java.sun.com/docs/books/tutorial/jdbc/index.html; especially the part about Prepared Statements.
 
Fidel Edwards
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:
You may want to read http://java.sun.com/docs/books/tutorial/jdbc/index.html; especially the part about Prepared Statements.



OOh Rob Tons of thanks !!
I can't tell you how happy I am . It was nice solution and tutorial.

Again thanks a lot .


 
reply
    Bookmark Topic Watch Topic
  • New Topic