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
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
Create your own Dateformatter...and sue your own Date Format to solve such problems.
Please edit your question if errors occur regarding Date Formating :-)
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.
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()));
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);
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.
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()));
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.