Age calculation using Apex and Visualforce

Age calculation using Apex and Visualforce

Sample code:


Visualforce page:


<apex:page controller=”Sample” >
<apex:form >
    <apex:pageblock >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Date:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:inputtext onfocus=”DatePicker.pickDate(true, this , false);” value=”{!dat}” id=”dt”/></apex:pageblockSectionItem>
            <apex:pageblockSectionItem >Age:</apex:pageblockSectionItem>                       
            <apex:pageblockSectionItem ><apex:outputText value=”{!age}” /></apex:pageblockSectionItem>           
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value=”Find” action=”{!find}”/>
        </apex:pageblockButtons>
    </apex:pageblock>
</apex:form>   
</apex:page>



Apex Controller:


public class Sample
{  
    public Integer age {get;set;}    
    public Date dat {get;set;}
   
    public void find()
    {
        Integer temp = dat.daysBetween(Date.Today());
        age = Integer.valueOf(temp/365);
    }
}



Output:

Leave a Reply