This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Create a Date object from String  RSS feed

 
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.

?
 
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!