开发者

Calendar object in Expression language

开发者 https://www.devze.com 2023-03-18 07:05 出处:网络
How do i get day, month and year (and every Calendar.XXXXXXX value) in expression lan开发者_如何学Cguage?

How do i get day, month and year (and every Calendar.XXXXXXX value) in expression lan开发者_如何学Cguage?

${object.calendarObject.MONTH}


You can use ${calendar.time} to get a Date object in EL. You can use JSTL <fmt:formatDate> to format a Date object into a human readable string in JSP. It uses SimpleDateFormat under the covers and supports also all of its patterns by the pattern attribute.

In below examples I assume for brevity that ${cal} is the calendar. Substitute it with your ${object.calendarObject} whenever applicable.

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<ul>
  <li>Standard date/time: <fmt:formatDate value="${cal.time}" type="both" /></li>
  <li>Standard date: <fmt:formatDate value="${cal.time}" type="date" /></li>
  <li>Day: <fmt:formatDate value="${cal.time}" pattern="d" /></li>
  <li>Month: <fmt:formatDate value="${cal.time}" pattern="M" /></li>
  <li>Year: <fmt:formatDate value="${cal.time}" pattern="yyyy" /></li>
  <li>dd-MM-yyyy: <fmt:formatDate value="${cal.time}" pattern="dd-MM-yyyy" /></li>
  <li>MM/dd/yyyy: <fmt:formatDate value="${cal.time}" pattern="MM/dd/yyyy" /></li>
</ul>

As of now this should yield something like (English locale, GMT+1):

  • Standard date/time: Jul 6, 2011 10:34:17 PM
  • Standard date: Jul 6, 2011
  • Day: 6
  • Month: 7
  • Year: 2011
  • dd-MM-yyyy: 06-07-2011
  • MM/dd/yyyy: 07/06/2011


You can't directly with the calendar object, you can create a wrapper for the Calendar object with getters methods like these:

public int getMonth(){
    wrappedCalendar.get(Calendar.MONTH);
}
public int getDay(){
    wrappedCalendar.get(Calendar.DAY_OF_MONTH);
}
public int getYear(){
    wrappedCalendar.get(Calendar.YEAR);
}
...

So you could use on your expression language something like this:

${calWrapper.month}/${calWrapper.day}/${calWrapper.year}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号