我在这里已经阅读了一些问题,但我仍然不明白如何“修剪”日期,JTable单元格(列)只有没有时间的日期。现在我在单元格中有日期和时间,但在DB中只保留日期,这就是为什么我到处都有 2013-04-02 00:00:00.0 ,但我想要 02-04-2013
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String Base = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=SL2.mdb";
con = DriverManager.getConnection(Base,"","");
st = con.createStatement();
res2 = st.executeQuery("SELECT * FROM Apmeklejumi");
rsmd = res2.getMetaData();
int column = rsmd.getColumnCount();
columnNames.addElement("ID");
columnNames.addElement("Iestašanas datums");
columnNames.addElement("Sūdzības");
columnNames.addElement("Diagnoze");
columnNames.addElement("Rekomendācijas");
columnNames.addElement("Pacienta ID");
while(res2.next()) {
Vector row = new Vector(column);
row.addElement(res2.getObject("ID"));
row.addElement(res2.getObject("Ies_dat")); // <--- date
row.addElement(res2.getObject("Sudz"));
row.addElement(res2.getObject("Diag"));
row.addElement(res2.getObject("Rekom"));
row.addElement(res2.getObject("Pacienta_ID"));
data.addElement(row);
}
st.close();
con.close();
} catch(Exception e) {
e.printStackTrace();
}
答案 0 :(得分:4)
默认情况下,Oracle教程How to Use Tables
将DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
用于XxxTableCellRenderer
例如
private static class DateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
if (!(value instanceof Date)) {
return this;
}
setText(DATE_FORMAT.format((Date) value));
return this;
}
}
修改
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class SimpleTableDemo extends JPanel {
private static final long serialVersionUID = 1L;
private boolean DEBUG = false;
private JTable table;
private JButton okButton;
private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian", "Date"};
private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), (false), new Date()},
{"John", "Doe", "Rowing", new Integer(3), (true), new Date()},
{"Sue", "Black", "Knitting", new Integer(2), (false), null},
{"Jane", "White", "Speed reading", new Integer(20), (true), new Date()},
{"Joe", "Brown", "Pool", new Integer(10), (false), new Date()},};
private TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
public SimpleTableDemo() {
super(new GridLayout(1, 0));
table = new JTable(model);
table.setRowHeight(20);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
TableColumnModel m = table.getColumnModel();
m.getColumn(5).setCellRenderer(new SubstDateRenderer());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
class SubstDateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
private Date dateValue;
private SimpleDateFormat sdfNewValue = new SimpleDateFormat("dd.MMMM.yyyy");
private String sdfNewValueString = "";
public SubstDateRenderer() {// formating TableCell
super();
setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
}
@Override
public void setValue(Object value) {
if ((value != null) && (value instanceof Date)) {
dateValue = (Date) value;
sdfNewValueString = sdfNewValue.format(dateValue);
value = sdfNewValueString;
}
super.setValue(value);
}
}
}
答案 1 :(得分:2)
见Table Format Renderers。给出了一个简单的例子来显示日期。
编辑:
例如,您可以使用:
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd");
TableCellRenderer renderer = new FormatRenderer( format );
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(???).setCellRenderer(renderer);
从我上面给你的链接中,不要忘记按照“相关阅读”部分中的链接。