Solved

Calculate age on JSP page

Posted on 2010-09-10
9
2,396 Views
Last Modified: 2012-05-10
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
 patientDemographics.patient.dob 

Open in new window

and
encounter.collectionDetails.collectionDate

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
0
Comment
Question by:marklorenz
  • 5
  • 4
9 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33646986
Have you had a look at this link?
http://forums.sun.com/thread.jspa?threadID=5272365

Sems it deals with exactly your problem.
0
 
LVL 12

Author Comment

by:marklorenz
ID: 33647296
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...
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33647365
I would strongly disencourage you to actually perform the calculation in the JSP itself. Have a look at this link:
http://stackoverflow.com/questions/102964/jstl-collection-iteration

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.
0
 
LVL 12

Author Comment

by:marklorenz
ID: 33647439
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33647560
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.
0
 
LVL 12

Accepted Solution

by:
marklorenz earned 0 total points
ID: 33648732
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;
		birthDate.setTime(patientDob);
		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))) {
			ageInYears--;
			}			
		ageAtCollection = Integer.toString(ageInYears) + " yrs";
	}		
%>
<%=ageAtCollection%>

Open in new window

0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33660494
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.
0
 
LVL 12

Author Comment

by:marklorenz
ID: 33661435
5 gets me a calendar, 9 sets date/time as i want
Guess a different constructor could have done that...
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33672464
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript Hashmap output 8 45
Javascript create file issue 4 47
Windows 10 IE Certificate Issue 10 41
Randomize in Owl Carousel v1.3.2 6 19
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now