且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JTable 中的日期字段格式

更新时间:2023-12-04 19:17:22

您需要自定义表模型来告诉 JTable 该列保存的是 Date 类型的值:

You need to customize the table model to tell the JTable that the column is holding values of type Date:

@Override
public Class getColumnClass(int col) {
    if (col == 3) {
        return java.util.Date.class;
    }
    // return the appropriate class for every column
}

这将使 JTable 使用渲染器以与当前语言环境相关联的格式格式化日期.如果您需要其他格式,则需要将另一个渲染器关联到该列,该列可以根据需要设置日期格式.

This will make the JTable use a renderer formatting the date with the format associated to the current locale. If you need another format, you need to associate another renderer to the column, which formats the date as you want to.