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ă, 14 noiembrie 2015

[OmniFaces utilities 2.0] Returns a new date instance which is a sum of the given date and the given amount of years, months, weeks, days, hours, minutes, seconds


[OmniFaces utilities] The addYears(), addMonths(), addWeeks(), addDays(), addHours(), addMinutes(), addSeconds() functions returns a new date instance which is a sum of the given date and the given amount of years, months, weeks, days, hours, minutes, seconds.

Functions:
Usage:

Let's suppose that the date comes from a simple bean:

@Named
@RequestScoped
public class DateBean {
   
 private Date date = new Date();
 private final String[] matches = {"Romania - Chile", "Argentina - Brazil", 
                                   "Chile - Brazil", "Romania - Argentina", "Romania - Brazil", "Argentina - Chile"};
   
 // getters & setters
}

And, let's consider the reference date as: 14-11-2015:10:05:58 (14 November 2015, time 10:05:58 AM).
Now, let's add some days, years, minutes...

Adding 1 year:
#{of:formatDate(of:addYears(dateBean.date, 1), "dd-MM-yyyy")}
Output: 14-11-2016
14 November 2015 become 14 November 2016

Adding 2 months:
#{of:formatDate(of:addMonths(dateBean.date, 2), "dd-MM-yyyy")}
Output: 14-01-2016
14 November 2015 become 14 January 2016

Adding 5 weeks:
#{of:formatDate(of:addWeeks(dateBean.date, 5), "dd-MM-yyyy")}
Output: 19-12-2015
14 November 2015 become 19 December 2015

Adding 1 day:
#{of:formatDate(of:addDays(dateBean.date, 1), "dd-MM-yyyy")}
Output: 15-11-2015
14 November 2015 become 15 November 2015

Adding 3 hours:
#{of:formatDate(of:addHours(dateBean.date, 3), "dd-MM-yy:HH:mm:ss")}
Output: 14-11-15:13:05:58
14 November 2015, 10:05:58 become 14 November 2015, 13:05:58

Adding 5 minutes:
#{of:formatDate(of:addMinutes(dateBean.date, 5), "dd-MM-yy:HH:mm:ss")}
Output: 14-11-15:10:10:58
14 November 2015, 10:05:58 become 14 November 2015, 10:10:58

Adding 30 seconds:
#{of:formatDate(of:addSeconds(dateBean.date, 30), "dd-MM-yy:HH:mm:ss")}
Output: 14-11-15:10:06:28
14 November 2015, 10:05:58 become 14 November 2015, 10:06:28

Adding 1 year, 1 month, 1 week, 1 day, 1 hour, 1 minute and 1 second:
#{of:formatDate(of:addYears(of:addMonths(of:addWeeks(of:addDays(of:addHours(of:addMinutes(of:addSeconds(dateBean.date, 1), 1), 1), 1), 1), 1), 1), "dd-MM-yy:HH:mm:ss")}
Output: 22-12-16:11:06:59
14 November 2015, 10:05:58 become 22 December 2016, 11:06:59

Display the next 7 days:
<c:forEach var="i" begin="0" end="4">
 #{of:formatDate(of:addDays(dateBean.date, i), "dd-MM-yyyy")}
</c:forEach>
Output: 14-11-2015   15-11-2015   16-11-2015   17-11-2015   18-11-2015

Display the next 12 months dates:
<c:forEach var="i" begin="0" end="11">
 #{of:formatDate(of:addMonths(dateBean.date, i), "dd-MM-yyyy")}
</c:forEach>
Output: 14-11-2015   14-12-2015   14-01-2016   14-02-2016   14-03-2016   14-04-2016   14-05-2016   14-06-2016   14-07-2016   14-08-2016   14-09-2016   14-10-2016

Let's schedule some soccer matches as 1 match / week:
<h:dataTable value="#{dateBean.matches}" var="match" binding="#{table}">
 <h:column>
  <f:facet name="header">Match</f:facet>
  #{match}
 </h:column>
 <h:column>
  <f:facet name="header">Date</f:facet>
  #{of:formatDate(of:addWeeks(dateBean.date, table.rowIndex+1), "dd-MM-yyyy")}
 </h:column>
</h:dataTable>

Output in figure below:

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors