java - 数据截断 : Incorrect datetime value: '' for column 'date' at row 1

标签 java mysql swing

我的应用程序出现此错误:

Data truncation: Incorrect datetime value: '2-24-2015' for column 'POrder_Date' at row 1

我有 MySQL 连接器 java v-5.1.7

java.util.Date date = new java.util.Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String date1, mon, datex, year, yearx, currentDate;
int d, d1;

以下代码在我的类的构造函数中:

date1=df.format(date);
    d=date1.indexOf('/');
    mon=date1.substring(0,d);
    d1=date1.lastIndexOf('/');
    datex=date1.substring(d+1,d1);
    yearx=date1.substring(d1+1);
    year="20"+yearx;
    currentDate=mon+"-"+datex+"-"+year;
    System.out.println("current date  "+currentDate);

最佳答案

mysql 默认日期格式“yyyy-mm-dd”。更改日期格式然后存储。可能会起作用。

java.util.Date date = new java.util.Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String  date1,mon,datex,year,yearx,currentDate;
int d,d1;
date1=df.format(date);
d=date1.indexOf('/');
mon=date1.substring(0,d);
d1=date1.lastIndexOf('/');
datex=date1.substring(d+1,d1);
yearx=date1.substring(d1+1);
year="20"+yearx;
currentDate=mon+"-"+datex+"-"+year;
System.out.println("current date  "+currentDate);
//change currentdate format MM-dd-yyyy into yyyy-MM-dd
try {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date convertedCurrentDate = sdf.parse(currentDate);
        System.out.println(new SimpleDateFormat("yyyy-MM-  dd").format(convertedCurrentDate));
    } catch (ParseException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

检查打印日期格式,如 (2015-05-25)。

关于java - 数据截断 : Incorrect datetime value: '' for column 'date' at row 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698744/

相关文章:

java - 字符串中有多少个非视觉符号

java - 在 wiremock 中,是否可以根据请求记录 cookie?

mysql - 更新连接字符串的每个值

php - 交叉表引用

java - 将链式方法与 Graphics2D 一起使用

java - 访问匿名内部类中封闭范围的成员变量

java - 开发服务器上的应用程序引擎数据存储统计信息

java - PathClassLoader 的实例是在何时何地在 android 源代码中创建的?

来自两行值等式的MySQL外键?

java - 使用 AssertJ 在没有测试失败的情况下在测试后关闭应用程序