This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi. I am trying to take a date, and parse it into the format: Aug 25, 2003 I created a SimpleDateFormat object and parsed the above string into a date object. But, once it parses to a date it reads: Mon Aug 25 00:00:00 CDT 2003 I am trying to display this information to a user in a table, so they are able to sort on it, so I need the data in a date format so it sorts correctly. The only way I could find to get the format I want is to return a String object. Is it possible to return a Date object in the format I want? Thanks for any help!
You will have to store the date internally to perform the sorting, but use the java.text.SimpleDateFormat when displaying it in the table. The "Mon Aug 25 00:00:00 CDT 2003" is the default format used in the .toString() method.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Feb 28, 2001
Thanks for the reply. When you say , "store the dates internally" what exactly do you mean by that? Right now, I have my model creating a new Date object for this column which is the default date format I am seeing now. So do I need to leave the model the way it is now, and in my custom renderer , tell it how to format the date with SimpleDateFormats format method, or I am I not even close to what you mean by storing the data internally? If that is what I need to do, does that make my table rendering slow down? What's the best way to do this? Thanks again for your help!!
Depending on how you set up your model class (and what you want to do), you can do a lot of different things. Since you want the user to be able to sort on the date in the table, your model should contain the actual java.util.Date object for sorting purposes. This date is not displayed, but it is there. Then, for displaying the String that represents the formatted date, you can either: 1). Pre-fill the information in the model or 2). Format the date as the table is being drawn. Option 1 is better for tables where the data is not going to change (report-style tables). It is faster. Option 2 is better for tables where the underlying data can change. It is much more dynamic--you don't have to remember to update the String object when you update the Date. Personally, I use option 2 without any problems, but my Swing programs are not really demanding ones (low-volume, single-user).