Jtable/ResultSet 中的日期格式
我无法在 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
当我们讨论 Date
时 code>JTables
... 我将 isCellEditable(.)
设置为 true,但无法编辑日期单元格。你如何做到这一点?
I am having trouble displaying Date
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要覆盖
getValue
,使用TableCellRenderer
相反:Do not override
getValue
, use aTableCellRenderer
instead:听起来像是日期的 toString() 表示形式存储在 TableModel 中,而不是日期对象中。因此,您需要检查数据如何从 ResultSet 复制到 TableModel。确保您使用的是 resultSet.getObject() 方法。或者问题可能是您在数据库中存储的字符串的格式与您看到的格式相同。
无论如何,一旦您能够在 TableModel 中实际存储 Date 对象,请查看 表格格式渲染器允许您在一行代码中创建具有自定义日期格式的自定义渲染器。
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.
您应该创建 DefaultTableCellRenderer 的子类并重写 setValue(Object) ,然后为整个列设置单元格渲染器。
然后在您的用户代码中执行类似
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.Then in your user code do something like
JTable.getColumnModel().getColumn(index).setCellRenderer(new DateCellRenderer());
有一个第三方插件可用.. moment.js。
只需通过 nuget 添加它,将脚本移动到您的内容脚本文件中。添加此行(示例)
Javascript:
然后我们更改 jtable 中的字段声明。
JavaScript:
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:
And then we change our field declaration in jtable.
Javascript: