calculate age

0
how i can calculate age from Birthdate ?
asked
3 answers
7

There is a YearsBetween function in the Community Commons module, you can download this module from the App store and use the YearsBetween function to determine the age.

answered
2

I used a simple Java action for that. Give two parameters to the Java action: currentDay (with the token end of current day) and the startDay (fill it with the birthdate).

This is the java code:

import com.mendix.systemwideinterfaces.core.UserAction;
import java.util.Calendar;
import java.util.Date;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

public class calculatedAge extends CustomJavaAction<Long>
{
    private java.util.Date startDay;
    private java.util.Date currentDay;

    public calculatedAge(IContext context, java.util.Date startDay, java.util.Date currentDay)
    {
        super(context);
        this.startDay = startDay;
        this.currentDay = currentDay;
    }

    @Override
    public Long executeAction() throws Exception
    {
        // BEGIN USER CODE
        try {
            return getAge(this.startDay, this.currentDay);
        } catch (Exception e) {
            return -1L;
        }
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public String toString()
    {
        return "calculatedAge";
    }

    // BEGIN EXTRA CODE
    public static long getAge(Date birthdate, Date comparedate) {
        if (birthdate == null)
            return -1L; 

        Calendar now = Calendar.getInstance();
        if (comparedate != null)
            now.setTime(comparedate);
        Calendar dob = Calendar.getInstance();
        dob.setTime(birthdate);
        if (dob.after(now)) 
            return -1L;

        int year1 = now.get(Calendar.YEAR);
        int year2 = dob.get(Calendar.YEAR);
        long age = year1 - year2;
        int month1 = now.get(Calendar.MONTH);
        int month2 = dob.get(Calendar.MONTH);
        if (month2 > month1) {
          age--;
        } else if (month1 == month2) {
          int day1 = now.get(Calendar.DAY_OF_MONTH);
          int day2 = dob.get(Calendar.DAY_OF_MONTH);
          if (day2 > day1) {
            age--;
          }
        }
        return age;     
    }
    // END EXTRA CODE
}

Good luck,

Ronald

answered
-1

Hi,

I just calculated the age with simple and short java action. Just pass the birthdate for this java action. This java action will return the age as a result.

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package voterregistration.actions;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

public class CalculateAge extends CustomJavaAction<java.lang.Long>
{
    private java.util.Date BirthDate;

    public CalculateAge(IContext context, java.util.Date BirthDate)
    {
        super(context);
        this.BirthDate = BirthDate;
    }

    @java.lang.Override
    public java.lang.Long executeAction() throws Exception
    {
        // BEGIN USER CODE
               
        LocalDate DOB = BirthDate.toInstant()
                .atZone(ZoneId.systemDefault())
              .toLocalDate();
        LocalDate curDate = LocalDate.now();
        Core.getLogger("Age").info(Period.between(DOB, curDate).getYears());
        return (long) Period.between(DOB, curDate).getYears();
        
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @java.lang.Override
    public java.lang.String toString()
    {
        return "CalculateAge";
    }

    // BEGIN EXTRA CODE
    // END EXTRA CODE
}
 

answered