• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Jtables with dates

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic