[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"};
"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