• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting date in JSP

 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use ${person.birthday} directly, it shows up as

"Wed Jun 11 00:00:00 EDT 1975"

How can I format it to something like "June 11, 1975"? By using an EL function call?

Thanks in advance!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do it using an EL function (unless you write your own). But there's no need since the <fmt:formatDate> action of the JSTL does it for you.

Any reason that you need to use an EL function as opposed to the JSTL?
[ April 09, 2007: Message edited by: Bear Bibeault ]
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah ok, thanks.

I'll just use <fmt:formatDate>.
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Related question:

I have a height value that's in inches. How can I format it so it's in feet and inches?

I tried the codes below but it gives 1 extra foot.

Example:
67 inches should be 5' 7"
but the code below gives 6' 7"

[ April 11, 2007: Message edited by: Wilson Gordon ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's rounding up due to the fractional digit setting. If you put in 65 inches, you get the expected result.

For the feet calculation, you'll need to do bit more math to subtract out the fractional part. Or you could write an EL function that does truncating division for you.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also subtract a foot if the modulus isn't zero.
 
Paul Clapham
Sheriff
Posts: 21116
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or do this to negate the rounding-up tendency:
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic