• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Formatting Help

 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course this would be much easier:

 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21430
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good,

Thanks to everyone for your help!


JP
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic