This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes Parsing a date and retaining the format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing a date and retaining the format" Watch "Parsing a date and retaining the format" New topic

Parsing a date and retaining the format

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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!
Mark Herschberg

Joined: Dec 04, 2000
Posts: 6037
The Javadoc for java.text.SimpleDateFormat provides some example patterns which you can base yours off of.
[ August 25, 2003: Message edited by: Mark Herschberg ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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!!
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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
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).
I agree. Here's the link:
subject: Parsing a date and retaining the format
It's not a secret anymore!