File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Date Formatting Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date Formatting Help" Watch "Date Formatting Help" New topic
Author

Date Formatting Help

John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Hello all,

I have a really stupid question concerning Date Formatting. It's driving me nuts and I can't seem to figure it out...

The situation is, I have known date (i.e. 06-30-05) and I can set that Date as a constant.

How would i convert a known date into a MM-DD-YY format?

please help,

Thanks in advance,

JP
Ivo Delver
Greenhorn

Joined: Jan 20, 2003
Posts: 7
im doing it this way



[ July 14, 2006: Message edited by: Ivo Delver ]
[ July 14, 2006: Message edited by: Ivo Delver ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Of course this would be much easier:

John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Thanks all for the help.

Ken, one question: what does myDate represent?

final SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(sdf.format(myDate));

i'm assuming myDate is my member variable?

Thanks,

JP
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

Originally posted by John Park:
Thanks all for the help.

Ken, one question: what does myDate represent?

final SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(sdf.format(myDate));

i'm assuming myDate is my member variable?

Thanks,

JP


For Ken's example to work, "myDate" should be a reference to a java.util.Date object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179



Regards, Rene Larsen
Dropbox Invite
John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Just to be sure, let me repeat my situation:

I have a known date (06-30-05) that I will be storing as a CONSTANT. I need the date to be in the above format (MM-DD-YY).

Isn't there a Calendar method out there that will convert that string to a date?"

Otherwise, it seems like parsing the string to a date format would be easiest.

Thanks,

JP
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by John Park:
Isn't there a Calendar method out there that will convert that string to a date?
No, that's what SimpleDateFormat is for. Calendar is for storing dates and doing calendar-like calculations.
Otherwise, it seems like parsing the string to a date format would be easiest.
Yes, that is what you should do.
John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Sounds good,

Thanks to everyone for your help!


JP
John Park
Greenhorn

Joined: Aug 04, 2005
Posts: 27
Hi,

Sorry to "reopen" this case, but isn't it better practice to throw Rene's string parsing code into a try/catch block?

Suggestions?

Thanks,


JP
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Only if you want to suppress the exception. Most likely it would be best to document clearly what exceptions may be thrown and then allow the client to handle it. What can you do? Nothing but suppress it and return null. What can the client do? Show the user an error, try a different format, substitute a default value, etc. Let the client decide how to handle invalid dates.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Formatting Help