我正在做java swing程序。为了让date和它的值存储在jtable中,我一直在使用Jdatechooser,但它只在我运行文件时起作用一次,但之后它就不起作用了,并显示了错误的日期。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date=sdf.format(jDateChooser2.getDate());jtable中的值
jDateChooser2.setDateFormatString((String) model.getValueAt(selectRow, 3));在数据库中存储数据的代码:
String e_id;
String type = type_exp.getSelectedItem().toString();
String amount = amnt.getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(jDateChooser2.getDate());
String disc = disc_.getText();
try {
String sql = "insert into expance(type_expance,amount,exp_date,disc) values('" + type + "','" + amount + "','" + date + "','" + disc + "')"; //"++" use this pattern to pass variables
int n = st.executeUpdate(sql); //use this for insert/update/delte query and for searching ExecuteQuery
if (n == 1) {
JOptionPane.showMessageDialog(this, n + " records saved successfully..");
} else {
JOptionPane.showMessageDialog(this, "something went wrong");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}获取jtable中的值的代码:
try {
String sql = "select * from expance";
rs = st.executeQuery(sql);
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
while (rs.next()) {
model.addRow(new Object[]{rs.getString("e_id"), rs.getString("type_expance"), rs.getString("amount"), rs.getString("exp_date"), rs.getString("disc")});
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}发布于 2020-07-03 16:47:07
您设置的是格式,而不是日期。
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse((String) model.getValueAt(selectRow, 3));
//Sets format
jDateChooser2.setDateFormatString(pattern );
//Sets the date
jDateChooser2.setDate(date );https://stackoverflow.com/questions/62709129
复制相似问题