• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting the date  RSS feed

 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have a string in this foramt :
Tue Dec 07 16:44:20 EST 2004
I want to convert it to a date of format :
mm.dd.yy
or
mm/dd/yy

I use following code, but it gives a ParseException.
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
format.setLenient(true);
Date date = format.parse(strDate);

Can anyone tell me what is wrong with above code ?

Thanks,
Gayatri
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about something like:

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should check the API docs for DateFormat and SimpleDateFormat to see the details about how these two classes work. In particular, the SimpleDateFormat docs have a table towards the top that explains what specifiers you can give in a valid format string.

Keep Coding!

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!