• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting MySQL Datetime to MS SQL Datetime and vice versa !  RSS feed

 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • 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 !
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 234
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 234
  • Mark post as helpful
  • send pies
  • 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 .


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!