• 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

Problems with Date comparator

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to display dates in a table and also have these dates sorted. Unfortunately, the extra catch is that I want the date to be formatted as just the dayname, month, day, and year (I don't want any time values). So, in my table for getValueAt(i,j) I return a DateFormat formatted string to display what I want. However, this returns a String object which screws up the comparator. eg: "Sunday July 15 2001" can actually be greater than "Monday September 18, 2004". 'S' is greater than 'M' but obviously 2004 is greater than 2001!

So, I think this kind of a silly way to do what I want. I created a new class called SimpleDate. It has a member called date of type java.util.Date. When I create a SimpleDate object, I have a Date object (such as from Calendar.getInstance().getTime()) and store it. My toString() method will display the date formatted the way I like. I made SimpleDate implement Comparable and I have a compareTo method which returns: this.date.compareTo(anotherSimpleDate.date)
(assuming all the objects are not null, etc)
Ideally, this will allow me to return a SimpleDate object in getValueAt(i,j) in my table model that is displayed properly and sorts correctly.

The sorting ability in my table is still messed up though. Can anyone suggest a better solution or any gotchas as to what might be wrong with my solution? I thought about displaying numbers like (2004/10/05) which makes the String comparable in the correct way but it doesn't look good so I don't want that.

Thanks.
[ September 24, 2004: Message edited by: Darien Cheung ]
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple ideas:

1) You could have redundant date columns. The Date object would be used for sorting, but hidden for display and the String date could be displayed.
2) Assuming your data structure contains all Objects - say Object[][] - then you could sort the data based on the Date column, and then after this is done you could format the Dates as a String and reassign these values to the column. You could even keep the original date column arround and reverse the process if you need to for future sorting. If possible the first solution is better.
 
reply
    Bookmark Topic Watch Topic
  • New Topic