Java Jtable/ResultSet 中的格式日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2412007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:16:08  来源:igfitidea点击:

Format Date in Jtable/ResultSet

javadatejtable

提问by twodayslate

I am having trouble displaying Dates in the format I want in my JTable. My JTablehas been created using a ResultSet and lists.

Date在我的JTable. MyJTable是使用 ResultSet 和列表创建的。

I tried the following in getValueAt(.)but no luck:

我尝试了以下getValueAt(.)但没有运气:

        if(value instanceof Date)
        {
            //System.out.println("isDate");
            DateFormat formatter = DateFormat.getDateInstance();
            SimpleDateFormat f = new SimpleDateFormat("MM/dd/yy");
            value = f.format(value);
            Date parsed  = (Date) value;
            try {
                parsed = (Date) f.parse(value.toString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            value = parsed.toString();
        }

The println(.)is never printed so it isn't even getting to that. The Format that is being displayed is Apr 10, 1992but I want 04/10/92

println(.)从不打印所以它甚至不说这个。正在显示的格式是Apr 10, 1992但我想要04/10/92

While we are on the topic of Datein JTables... I have isCellEditable(.)as true but I cannot edit the Date cells. How do you do this?

当我们讨论Datein的主题时,JTablesisCellEditable(.)确实如此,但我无法编辑日期单元格。你怎么做到这一点?

采纳答案by Peter Lang

Do not override getValue, use a TableCellRendererinstead:

不要覆盖getValueTableCellRenderer而是使用 a :

TableCellRenderer tableCellRenderer = new DefaultTableCellRenderer() {

    SimpleDateFormat f = new SimpleDateFormat("MM/dd/yy");

    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        if( value instanceof Date) {
            value = f.format(value);
        }
        return super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column);
    }
};

table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);

回答by camickr

The Format that is being displayed is Apr 10, 1992

显示的格式是 1992 年 4 月 10 日

Sounds like a toString() representation of the Date is being stored in the TableModel and not a Date Object. So you need to check how your data is copied from the ResultSet to the TableModel. Make sure you are using the resultSet.getObject() method. Or maybe the problem is that you are storing a String in your database that is formatted the way you see it.

听起来像 Date 的 toString() 表示被存储在 TableModel 而不是 Date 对象中。所以你需要检查你的数据是如何从 ResultSet 复制到 TableModel 的。确保您使用的是 resultSet.getObject() 方法。或者,问题可能在于您在数据库中存储了一个字符串,该字符串的格式与您看到的一样。

Anyway, once you are able to actually store a Date object in the TableModel, check out Table Format Rendererswhich allows you to create a custom renderer with a customized date format in a single line of code.

无论如何,一旦您能够在 TableModel 中实际存储 Date 对象,请查看Table Format Renderers,它允许您在一行代码中创建具有自定义日期格式的自定义渲染器。

回答by ntm

You should create a subclass of DefaultTableCellRenderer and override setValue(Object)then set the cell renderer for the whole column.

您应该创建 DefaultTableCellRenderer 的子类并覆盖setValue(Object)然后为整列设置单元格渲染器。

public class DateCellRenderer extends DefaultTableCellRenderer {
    public DateCellRenderer() { super(); }

    @Override
    public void setValue(Object value) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");

        setText((value == null) ? "" : sdf.format(value));
    }
}

Then in your user code do something like JTable.getColumnModel().getColumn(index).setCellRenderer(new DateCellRenderer());

然后在您的用户代码中执行类似的操作 JTable.getColumnModel().getColumn(index).setCellRenderer(new DateCellRenderer());

回答by Ze Knight

There is a third party add on available.. moment.js.

有一个第三方插件可用.. moment.js

Just add it via nuget, move the scripts to your content scripts file. Add this line (example)

只需通过 nuget 添加它,将脚本移动到您的内容脚本文件中。添加这一行(示例)

Javascript:

Javascript:

<script src="@Url.Content("~/Content/Scripts/moment.min.js")" type="text/javascript"></script>

And then we change our field declaration in jtable.

然后我们在 jtable 中更改我们的字段声明。

Javascript:

Javascript:

DateAdded: {
                        title: 'Date added',
                        width: '20%',
                        sorting: false,
                        display: function (data) {
                            return moment(data.record.DateAdded).format('DD/MM/YYYY HH:mm:ss');
                        }
                    }