• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a Date object from String  RSS feed

 
chris gar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following String "12/22/1959 11:45:00"

I need to turn this into a Date object, then write to a mysql database.

I parse out the values to int and use the

but when I try to insert to mysql, the date is "Wed Dec 07 AD 11:45:00 CDT"

I don't get a sql error - it just doesn't write anything - just 0000-00-00 00:00:00.

?
 
Julia Reynolds
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,

You can use the SimpleDateFormat class to transform a Date into a formatted
string that should be acceptable to mySQL. http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

Here is an example:
public String paintSystemTimeStamp(){
String timestamp = "";

String dateformat = "MM-dd-yyyy HH:mm:ss a z";
timestamp = (new SimpleDateFormat(dateformat)).format(new Date());

return timestamp;

}

If this were for an Oracle db, you might use the to_date SQL function to convert a string into a date. Is there an equivalent function in mySQL?

Julia
[ January 11, 2005: Message edited by: Julia Reynolds ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!