Java Date Concept

I use 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 save these dates in the mysql database table column table

am using this code

String datevalue=request.getParameter("date");

this date value prints as follows 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));

When I enter the time, I get this error

java.lang.IllegalArgumentException

introduce me how to solve this problem and save in the database

+4
source share
7 answers

You want to use a date format object to parse dates like 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); 
}
+1
source

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 paste dtinto the database. There must be a space after the comma (,) in the method split(for example, split(", ")).

+1

STR_TO_DATE. typeCasting INSERT :

INSERT INTO MY_TABLE VALUE (STR_TO_DATE('5/15/2012 8:06:26 AM', '%c/%e/%Y %r'))
0

JODA, , ; SimpleDateFormat JODA .

0

java.lang.IllegalArgumentException datetokens. blank space - split(","). trim()

for(String token: datetokens){
java.sql.Date dt = java.sql.Date.valueOf(new String(token.trim()));
}
0

: -

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

:

  • (,) .
    String datevalue=request.getParameter("date");

  • , .
    String[] datetokens = datevalue.split(",");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", dateValue);
    ... , .

  • ( ) .
    for(String dateValue : datetokens){
    Date date = sdf.parse(dateValue.trim());
    java.sql.Date sqlDate = new java.sql.Date(date.getTime());
    // INSERT sqlDate MySQL .
    }

  • Please do not forget to manage the connection to the database and provide the correct request upon insertion.

  • In addition, handle ParseExceptioni.e. either use try / catch or throw.

0
source

All Articles