• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date conversion.  RSS feed

 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!