Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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);
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic