Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Jtables with dates

 
Cy Bird
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a column in a JTable that holds a date value in mm/dd/yyyy format. The actual value in the database is a java.util.Date, but we convert it to a string using a SimpleDateFormat to get it into the mm/dd/yyyy format.
In string form though, it doesn't sort correctly: 11/10/2003 comes before 11/11/2002. When I leave it a date, it sorts correctly, but displays in the wrong format: MMM dd, yyyy.
Does anyone know a way around this?
Thanks.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store it as a Date and use a cell renderer to display it as a String in the format you want.
 
Cy Bird
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you. I found my way there...
For anyone else with this problem, I ended up with something like this:
getTableCellRendererComponent() {
SimpleDateFormat dateFormat_mmddyy = new SimpleDateFormat("MM-dd-yyyy");
String formatted = dateFormat_mmddyy.format((Date)value);
return new JLabel(formatted);
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic