Date format in Jtable / ResultSet

I'm having trouble displaying Date in the format I want in my JTable . My JTable was created using 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(); } 

println(.) never prints, so it doesn't even get to that. Display format Apr 10, 1992 but I want 04/10/92

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

+4
source share
4 answers

Do not override getValue , instead of 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); 
+14
source

Display Format April 10, 1992

It looks like the toString () Date view is stored in a TableModel and not in a Date object. Therefore, you need to check how your data is copied from ResultSet to TableModel. Make sure you use the resultSet.getObject () method. Or maybe the problem is that you are storing a row in your database, which is formatted the way you see it.

In any case, as soon as you can actually save the Date object in TableModel, go to Table Format Renderers , which allows you to create custom renderers with a custom date format in one line of code.

+5
source

You must subclass DefaultTableCellRenderer and override setValue(Object) , and then set the cell renderer to the entire 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());

+4
source

There is an additional third party file. moment.js .

Just add it via nuget, move the scripts to the content script 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'); } } 
-5
source

Source: https://habr.com/ru/post/1303598/


All Articles