JDatechooser 显示不正确的日期
JDatechooser shows incorrect date
我正在研究 java 摇摆程序。
有日期并将它的值存储在 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());
}
您设置的是格式而不是日期。
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 );
我正在研究 java 摇摆程序。 有日期并将它的值存储在 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());
}
您设置的是格式而不是日期。
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 );