[OmniFaces utilities] The
yearsBetween()
function returns the amount of years between two given dates. The monthsBetween()
function returns the amount of months between two given dates. The weeksBetween()
function returns the amount of weeks between two given dates. The daysBetween()
function returns the amount of days between two given dates.Functions:
Usage:
Helper bean to define two dates:
@Named
@RequestScoped
public class DateBean {
private Date
firstDate;
private Date
secondDate;
// not thread-safe
SimpleDateFormat
formatter = new SimpleDateFormat("dd-MMM-yyyy");
String
firstDateInString = "7-Jun-2013";
String
secondDateInString = "11-Dec-2022";
public DateBean() {
try {
firstDate =
formatter.parse(firstDateInString);
secondDate =
formatter.parse(secondDateInString);
} catch (ParseException ex) {
//...
}
}
public Date
getFirstDate() {
return firstDate;
}
public Date
getSecondDate() {
return secondDate;
}
}
Now let's see some examples:
Now let's see some examples:
This event will last from #{dateBean.firstDate} to
#{dateBean.secondDate}:
#{of:yearsBetween(dateBean.firstDate, dateBean.secondDate)} years!
#{of:monthsBetween(dateBean.firstDate, dateBean.secondDate)} months!
#{of:weeksBetween(dateBean.firstDate, dateBean.secondDate)} weeks!
#{of:daysBetween(dateBean.firstDate, dateBean.secondDate)} days!
Output:
This event will last from Fri Jun 07 00:00:00 EEST 2013 to
Sun Dec 11 00:00:00 EET 2022:
10 years!
115 months!
497 weeks!
3474 days!
You can use this information for creating
different kinds of loops. Per example:
Display years between using ui:repeat:
<ui:repeat
value="#{of:createArray(of:yearsBetween(dateBean.firstDate,
dateBean.secondDate)+1)}" varStatus="i">
Year: #{i.index+1}
(#{dateBean.firstDate.year + 1900 + i.index}): ...
</ui:repeat>
Display years between using c:forEach:
<c:forEach var="i" begin="0"
end="#{of:yearsBetween(dateBean.firstDate, dateBean.secondDate)}">
Year: #{i+1}
(#{dateBean.firstDate.year + 1900 + i}): ...
</c:forEach>
Output:
Year: 1 (2013): ...
Year: 2 (2014): ...
Year: 3 (2015): ...
Year: 4 (2016): ...
Year: 5 (2017): ...
Year: 6 (2018): ...
Year: 7 (2019): ...
Year: 8 (2020): ...
Year: 9 (2021): ...
Year: 10 (2022): ...
Year: 11 (2023): ...
Niciun comentariu :
Trimiteți un comentariu