Reputation: 41
I have util dates like 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23
I want to store that dates into mysql database table column date
am using these code
String datevalue=request.getParameter("date");
this datevalue is printing like this 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23
.
String[] datetokens=datevalue.split(",");
java.sql.Date dt = java.sql.Date.valueOf(new String(datevalue));
In inserting time am getting these error
java.lang.IllegalArgumentException
give me suggetion howto solve that problem and stored in database
Upvotes: 4
Views: 116
Reputation: 3450
The following code will print each date.
String datevalue = "2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23, 2013-10-23";
String[] datetokens = datevalue.split(", ");
for (String each : datetokens) {
java.sql.Date dt = java.sql.Date.valueOf(each);
System.out.println(dt);
}
You can insert dt
to the database. There must be a space after comma (,) in split
method (like split(", ")
).
Upvotes: 1
Reputation: 1705
In this case:
You passed a comma (,) seperated list of date values.
String datevalue=request.getParameter("date");
splited all the dates into an array of strings holding each date value.
String[] datetokens = datevalue.split(",");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", dateValue);
...specify the date format to be parsed.
now you need to iterate (for loop) to access each of strings in the array.
for(String dateValue : datetokens){
Date date = sdf.parse(dateValue.trim());
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
// here INSERT the sqlDate
value into MySQL for each iteration.
}
Please don't forget to manage database connection and provide proper query while inserting.
Also, handle the ParseException
ie. either use try/catch or throws.
Upvotes: 0
Reputation: 15035
Try like this as well:-
String dateValuesString = request.getParameter("date");
String[] dateValueStrings = dateValuesString.split(",");
SimpleDateFormat myFormat = new SimpleDateFormat("yy-MM-dd");
for (String dateString : dateValueStrings) {
String reformattedStr = myFormat.format(dateString);
System.out.println(reformattedStr);
}
Upvotes: 0
Reputation: 21981
java.lang.IllegalArgumentException
is due to wrong format of datetokens
. It get blank space
because of split(","
). Use trim()
method
for(String token: datetokens){
java.sql.Date dt = java.sql.Date.valueOf(new String(token.trim()));
}
Upvotes: 0
Reputation: 44881
You want to use a date format object to parse the dates such as SimpleDateFormat
String dateValuesString = request.getParameter("date");
String[] dateValueStrings = dateValuesString.split(",");
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
for (String dateString : dateValueStrings) {
Date date = sdf.parse(dateString);
}
Upvotes: 1
Reputation: 577
I would actually use JODA to do what you need there; however, SimpleDateFormat or JODA will do the trick.
Upvotes: 0
Reputation: 13465
You should use STR_TO_DATE
function.
Try typeCasting at the time of INSERT like this:
INSERT INTO MY_TABLE VALUE (STR_TO_DATE('5/15/2012 8:06:26 AM', '%c/%e/%Y %r'))
Upvotes: 0