简体   繁体   中英

Java date concept

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

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); 
}

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(", ") ).

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'))

I would actually use JODA to do what you need there; however, SimpleDateFormat or JODA will do the trick.

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()));
}

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM