Calculate age on JSP page

I am trying to change the patient age on a page to show up relative to the collection date (instead of the current date).  I have available

Open in new window


Open in new window


This is how the age is currently rendered:
<li class="report-age h5 bold">${patientDemographics.patient.age} <c:if test="${patientDemographics.patient.age != 'n/a'}" >yrs</c:if></li>

Open in new window

I see the collection date being rendered this way:
<fmt:formatDate value="${encounter.collectionDetails.collectionDate}"  pattern="MM/dd/yyyy"/>

Open in new window

My question:  how do I use JSTL or something else to do this in a "clean" way on this JSP page?  (The POJOs related to this make it not so simple to try to do in, e.g., the Patient class)

I.e. the goal is to show the age as an integer based on:
collection date - date of birth
LVL 12
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Have you had a look at this link?

Sems it deals with exactly your problem.
marklorenzAuthor Commented:
Yeah, I had looked at that, but they are relying on code in the Patient class - in my system, patient doesn't have the other date for the calculation.  I'm trying to stay local to the JSP...
I would strongly disencourage you to actually perform the calculation in the JSP itself. Have a look at this link:

Here they describe how you can register an EL Function. EL functions allow you to call a public static method in a public class.
This way you could do the calculation in a separate class and use the result in your JSP.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

marklorenzAuthor Commented:
Yeah I had seen that reallly nice post on stack overflow.  Again, the only issue is that I don't have the information available.  I am trying to avoid changes to my POJOs.  I see multiple ways if I do that work.  This would be the most straightforward, possibly this?

<fmt:formatNumber type="number" maxIntegerDigits="3" 
 value="${encounter.collectionDetails.collectionDate  - patientDemographics.patient.dob” />

Open in new window

Well you wouldn't have changed a single class. You would have added one to your project without changing anything else.

If you want, you could to this:
(encounter.collectionDetails.collectionDate.getTime() - patientDemographics.patient.dob.getTime())/(1000*60*60*24*365)
This converts every date to its millisecs, calculates the difference and then calculates the number of years ... but this is a really rough calculation ... I would certainly recommend to use Javas Calendar class, as this does really good date calculations.
marklorenzAuthor Commented:
This is a bit less than ideal, but I am on the Calendar path.  Will see if it works...
//Calculate age for results pages based on (collection date - patient DOB)
	int ageInYears;
	String ageAtCollection = "n/a";
	Calendar collectionDate = Calendar.getInstance().setTime(encounter.getCollectionDetails().getCollectionDate());
	Calendar birthDate =  = Calendar.getInstance().setTime(encounter.getPatient().getDob());
	if (birthDate != null){
		Date patientDob = this.dob;
		ageInYears = collectionDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);		
		if ((birthDate.get(Calendar.MONTH) > collectionDate.get(Calendar.MONTH))
				|| (birthDate.get(Calendar.MONTH) == collectionDate.get(Calendar.MONTH) && birthDate.get(Calendar.DAY_OF_MONTH) > collectionDate
					.get(Calendar.DAY_OF_MONTH))) {
		ageAtCollection = Integer.toString(ageInYears) + " yrs";

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Why exactly are you setting the birthDate in line 5 and are then re-setting it in line 9? But otherwise it looks pretty much like correct and should do for your task.
marklorenzAuthor Commented:
5 gets me a calendar, 9 sets date/time as i want
Guess a different constructor could have done that...
If your problem is solved, it would be great if you would close this question, as the thread has gone to my "second tab" and I won't directly see any changes anymore. If you still have problems I'll do my best to assist you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.