Convert String to Date?
I have a string variable like this
and I would like to change it to Date format compatable with the date format in MySql database which is also like 'YYYY-MM-dd'
could you please help ?
what should I use ????
I use the following code
the output for this code was
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date s1 =dateFormat.parse(s);
Thu Nov 25 00:00:00 GMT 2010
it is horrible ! how can I save this date into MySQL (because I want to change the string to date but not this format I want it to be the same format I mentioned above)
please please help me!!
I don't use SQL, but I do know that SimpleDateFormat returns a java.util.Date object, which I believe can be turned into a java.sql.Date object by using the latter's constructor that takes a long parameter.
Also, please understand that the java.util.Date object that you've created does not have a format. The String that you think is ugly is simply the toString representation of the Date object and nothing more. It may take some effort but you need to separate the idea of Date and String representation of Date as two separate and distinct entities.
thank you very much
so could you please tell me how to convert from java.util.Date to java.sql.Date?
Again, I don't use SQL, but just looking at the API, perhaps you could do
java.sql.Date mySqlDate = new java.sql.Date(myUtilDate.getTime());
That's the usual method for doing it.