• 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 conversion.

 
Ranch Hand
Posts: 99
  • 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 date coming as string in yyyyMMddformat.(eg.20110810).
I would like to convert it into a String in the format, Aug. 08 2011.

Any ideas?

P.S : This is not a homework project. I need this to dislay on a jsp page.

Thanks.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use one SimpleDateFormat to parse the incoming String into a Date, and use another SDF to format that Date into a String of the desired format.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Use one SimpleDateFormat to parse the incoming String into a Date...


And furthermore, keep and use it as a Date internally.

The chances are you have no choice as to how the data is coming in, but it's bad practise to use Strings as substitutes for other types (in this case: Date; or possibly Calendar).

Winston
 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone.
Below is the code how I did it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Viidhya Kishore wrote:Thanks everyone.
Below is the code how I did it.



Looks pretty good. Just a couple of points to consider:

1. I would have broken it up into multiple lines. It's easier to read that way, and there's nothing gained by jamming it all into one line.
2. I would rename the input String from "date" to "dateStr" or "inputDateStr" or something.


3. The coment //format = MMMM d, yyyy. is confusing and serves no purpose.

4. Catching an exception and then just returning null is not usually the way to go. It's subverting the entire purpose of the exception mechanism. Just catching the exception doesn't fix anything. If you're not going to actually fix the problem, don't bother catching the exception, or at least wrap and re-throw it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use one single date format object:
It's basically the same as what Jeff wrote, but it will reuse the existing object.
reply
    Bookmark Topic Watch Topic
  • New Topic