Jtable/ResultSet 中的日期格式

发布于 2024-08-24 02:41:33 字数 995 浏览 3 评论 0原文

我无法在 JTable 中以我想要的格式显示 Date。我的 JTable 是使用 ResultSet 和列表创建的。

我在 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();
        }

println(.) 从未被打印,因此它甚至没有达到这一点。显示的格式是 Apr 10, 1992 但我想要 04/10/92

当我们讨论 Datecode>JTables... 我将 isCellEditable(.) 设置为 true,但无法编辑日期单元格。你如何做到这一点?

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

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

        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, 1992 but I want 04/10/92

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滥情空心 2024-08-31 02:41:33

不要覆盖 getValue,使用 TableCellRenderer 相反:

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);

Do not override getValue, use a TableCellRenderer instead:

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);
温柔戏命师 2024-08-31 02:41:33

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

听起来像是日期的 toString() 表示形式存储在 TableModel 中,而不是日期对象中。因此,您需要检查数据如何从 ResultSet 复制到 TableModel。确保您使用的是 resultSet.getObject() 方法。或者问题可能是您在数据库中存储的字符串的格式与您看到的格式相同。

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

The Format that is being displayed is
Apr 10, 1992

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.

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

被翻牌 2024-08-31 02:41:33

您应该创建 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));
    }
}

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

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

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());

一念一轮回 2024-08-31 02:41:33

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

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

Javascript:

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

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

JavaScript:

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

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

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

Javascript:

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

And then we change our field declaration in jtable.

Javascript:

DateAdded: {
                        title: 'Date added',
                        width: '20%',
                        sorting: false,
                        display: function (data) {
                            return moment(data.record.DateAdded).format('DD/MM/YYYY HH:mm:ss');
                        }
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文