我的应用程序出现此错误:
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/