Play Games

Search This Blog

Thursday, November 19, 2015

Display all months between two given dates in Visualforce Page

Apex Class:
public class DisplayAllMonthsBetweenDates {
    public list<Date> monthYearList {get;set;}
    public date startDate {get;set;}
    public date endDate {get;set;}
    public DisplayAllMonthsBetweenDates () {
        Set<Date> monthYearSet = new Set<Date>();
        monthYearList = new list<Date>();
        startDate = date.newInstance(2015, 1, 17);
        endDate = date.newInstance(2015, 11, 17);
        while(startDate <= endDate) {
            monthYearSet.add(startDate);
            startDate = startDate.AddMonths(1);
        }
        monthYearList.addAll(monthYearSet);
        monthYearList.sort();
    }
}
Visualforce Page:
<apex:page controller="DisplayAllMonthsBetweenDates">
    <apex:pageBlock title="Months between 17-1-2015 to 17-11-2015">
        <apex:pageBlockTable value="{!monthYearList }" var="newDate">
            <apex:column>
                <apex:outputText value="{0,date,MMM-YYYY}">
                <apex:param value="{!newDate}" />
                </apex:outputText>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>

</apex:page>
Output:

No comments:

Post a Comment