简体   繁体   English

Java日期概念

[英]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 我的使用日期如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 我想将该日期存储到mysql数据库表列日期中

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 . 此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(",");
java.sql.Date dt = java.sql.Date.valueOf(new String(datevalue));

In inserting time am getting these error 在插入时间中遇到这些错误

java.lang.IllegalArgumentException 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 您要使用日期格式对象来分析日期,例如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. 您可以将dt插入数据库。 There must be a space after comma (,) in split method (like split(", ") ). split方法中逗号(,)后面必须有一个空格(例如split(", ") )。

You should use STR_TO_DATE function. 您应该使用STR_TO_DATE函数。 Try typeCasting at the time of INSERT like this: 像这样在INSERT时尝试typeCasting:

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; 我实际上将使用JODA来完成您在那里需要的工作; however, SimpleDateFormat or JODA will do the trick. 但是,SimpleDateFormat或JODA可以解决问题。

java.lang.IllegalArgumentException is due to wrong format of datetokens . java.lang.IllegalArgumentException是由于datetokens格式错误。 It get blank space because of split("," ). 由于split(","split(","它获得blank space Use trim() method 使用trim()方法

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循环)以访问数组中的每个字符串。
    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. //在此,将每次迭代的sqlDate值插入MySQL。
    }

  • Please don't forget to manage database connection and provide proper query while inserting. 请不要忘记管理数据库连接并在插入时提供正确的查询。

  • Also, handle the ParseException ie. 另外,处理ParseException即。 either use try/catch or throws. 使用try / catch或throws。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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