Rob Camick
关于表格格式渲染器的帖子可能会解决您的问题。
更新:
我尝试了一个使用mKorbel代码的示例(因为我也很想看看DateFormat
到目前为止我还没有做过的 JTable) 。我给出的格式是.input
"2013-01-25"
import java.awt.Dimension;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class JTableDateFormat {
public static void main(String[] args) {
Object[][] data = {
{"Amar", "2013-01-25"},
{"Sammy", "2013-01-25"}
};
Object[] columnNames = {"Name", "Date"};
JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(1).setCellRenderer(new DateRenderer());
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.setSize(new Dimension(400, 100));
frame.setVisible(true);
}
}
class DateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
private Date dateValue;
private SimpleDateFormat sdfNewValue = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy");
private String valueToString = "";
@Override
public void setValue(Object value) {
if ((value != null)) {
String stringFormat = value.toString();
try {
dateValue = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH).parse(stringFormat);
} catch (ParseException e) {
e.printStackTrace();
}
valueToString = sdfNewValue.format(dateValue);
value = valueToString;
}
super.setValue(value);
}
}