• 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

Date Formatting Help

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course this would be much easier:

 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic