tags:

views:

233

answers:

7

Hi there.

I have a String with the following format: january_2005 (MMMMMMM_yyyy)

and i want to convert it to a mysql acceptable date value to insert it on the database.

I need to do it on Java.

Does anyone knows how to do it?

Thanks

A: 

Create your own Dateformatter...and sue your own Date Format to solve such problems.

DateFormatter

DateFormat

Please edit your question if errors occur regarding Date Formating :-)

bastianneu
A: 

You could so something with SimpleDateFormat like this to get a Date object:

DateFormat formatter = new SimpleDateFormat("MMMM_yyyy");
Date date = (Date)formatter.parse("January_2009");

You may need to uppercase the first letter of 'january' to get this to work (I'm not sure what range of inputs SimpleDateFormat accepts.

Once you've got a Date, it depends on how you're storing dates in MySQL as to what to do next.

Dominic Rodger
A: 

I think something like this would do it using prepared statement

DateFormat formatter = new SimpleDateFormat("MMMMMMM_yyyy");
Date mydate = formatter.parse("january_2005");
stmt_date.setTimestamp(4, new Timestamp(mydate.getTime()));
Tommy
What's with the 7 Ms?
Dominic Rodger
+1  A: 

Something like this will probably do the trick,

SimpleDateFormat myFormat = new SimpleDateFormat("MMMM_yyyy");
Date aDate;
try 
{
    aDate = myFormat.parse("January_2009");
} 
catch (ParseException e) 
{
    // Error handling
}

You can then insert aDate into your DB using a PreparedStatement.

If you're not using PreparedStatements and need it as a String instead, then you can just do the following. Although, as mentioned in the comments, it's much safer to use PreparedStatements for inserting into a DB.

SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd hh:dd:ss");
String sqlDateString = aDate.format(sqlFormat);
Rich Adams
it's very bad using a string to insert a Date into a DB using SQL. You should always use PreparedStatements.
Nick Fortescue
Agreed. But the question just asked how to convert it to an acceptable value, not how to insert it into a DB. Which is why I provided the option to convert to a string at the end. I will update my answer to make this clearer though, as you're right that it's bad practice.
Rich Adams
This does not work in non-English localized machines.
BalusC
A: 

First parse the date into a Date object using SimpleDateFormat:

 Date d=  new SimpleDateFormat("MMMM_yyyy").parse(s)

Then insert the date using parameterised SQL (a PreparedStatement):

public insertData(Date d) {
   Connection conn = setupTheDatabaseConnectionSomehow();
   PreparedStatement stmt = 
     conn.prepareStatement("INSERT INTO t (date) values (?)");
   stmt.setDate(2, new java.sql.Date(d));
   stmt.executeUpdate();
}

The question marks will be automatically converted into the correct format for MySQL, and will make your code more secure and more portable. Search for parameterised SQL for more info.

Nick Fortescue
+3  A: 

According to the SimpleDateFormat API the pattern is actually MMMM_yyyy.

So to get a worthfully java.util.Date out of this String you need to do this:

String stringDate = "january_2005";
Date date = new SimpleDateFormat("MMMM_yyyy").parse(stringDate);

If those months are expected to be always English and the Locale of the machine where it runs isn't (always) English, then you better need to specify the Locale as well:

Date date = new SimpleDateFormat("MMMM_yyyy", new Locale("en")).parse(stringDate);

To save it in the database, use PreparedStatement#setDate(). You'll need to convert java.util.Date to java.sql.Date first.

preparedStatement = connection.prepareStatement("INSERT INTO mytable (somedate) VALUES (?)";
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));
BalusC
Amazing how many forget the importance of `Locale`. No one answer mentiones the `Locale`. In non-English machines you would get a `ParseException` on `january` otherwise.
BalusC
A: 

The answers here suggesting SimpleDateFormat largely look good. However, to (possibly) complicate the issue, note that SimpleDateFormat is not thread safe. If this is a potential issue for you the check out the Joda time library, which has similar formatting classes but with a thread-safe guarantee.

Brian Agnew