vineri, 27 noiembrie 2015

[OmniFaces utilities 2.0] A mapping of month names (full/short names) by month numbers for the current locale


[OmniFaces utilities] The getMonths() function returns a mapping of month names by month numbers for the current locale. For example: "January=1", "February=2", etc. This is useful if you want to for example populate a <f:selectItems> which shows all months. The locale is obtained by Faces#getLocale(). The mapping is per locale stored in a local cache to improve retrieving performance.

[OmniFaces utilities] The getShortMonths() function returns a mapping of short month names by month numbers for the current locale. For example: "Jan=1", "Feb=2", etc. This is useful if you want to for example populate a <f:selectItems> which shows all short months. The locale is obtained by Faces#getLocale(). The mapping is per locale stored in a local cache to improve retrieving performance.

[OmniFaces utilities] The getMonth() function returns the month name from the mapping associated with the given month number for the current locale. For example: "1=January", "2=February", etc. The locale is obtained by Faces#getLocale().

[OmniFaces utilities] The getShortMonth() function returns the short month name from the mapping associated with the given month number for the current locale. For example: "1=Jan", "2=Feb", etc. The locale is obtained by Faces#getLocale().

Function (full names):

Function (short names):

Functions (get month name (full/short name) by month number)

Usage:

In OmniFaces Showcase you can see how an example of how to populate a <f:selectItems/> which shows all months. Below, you can see an example of displaying the month numbers and names in a data table.

Full names via of:getMonths():

<h:dataTable value="#{of:getMonths().entrySet()}" var="t">
 <h:column>
  <f:facet name="header">No.</f:facet>
  #{t.value}
 </h:column>
 <h:column>
  <f:facet name="header">Name</f:facet>
  #{t.key}
 </h:column>
</h:dataTable>

Short names via of:getShortMonths():

<h:dataTable value="#{of:getShortMonths().entrySet()}" var="t">
 <h:column>
  <f:facet name="header">No.</f:facet>
  #{t.value}
 </h:column>
 <h:column>
  <f:facet name="header">Name</f:facet>
  #{t.key}
 </h:column>
</h:dataTable>

Moreover, we can obtain a month name (short name) by its number via of:getMonth() and of:getShortMonth():

The 10 month of the year is: #{of:getMonth(10)}
Output: October
The 10 month of the year is: #{of:getShortMonth(10)}
Output: Oct

The month number can be provided from a managed bean also:

#{of:getMonth(fooBean.monthnumer)}
#{of:getShortMonth(fooBean.monthnumer)}

Niciun comentariu:

Trimiteți un comentariu