My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 21 noiembrie 2015

[OmniFaces utilities 2.0] Returns the amount of years/months/weeks/days between two given dates


[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:

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors