Link to home
Start Free TrialLog in
Avatar of FastEddie___
FastEddie___

asked on

Code Conversion from Java to Coldfusion

I'm trying to find a more accurate way of calculating a persons age at a certain point in time.

The result must be in Years and Months and should be accurate and I'm trying to use the Java gregorian calendar.

Can someone please help me translate this java code to coldfusion?

Here is the Java code I would like to convert:

--------------------------------------------------------------------

Term difference (Date from, Date to) {
    GregorianCalendar cal1 = new GregorianCalendar();
    cal1.setTimeInMillis(from.getTime());
 
    GregorianCalendar cal2 = new GregorianCalendar();
    cal2.setTimeInMillis(to.getTime());
 
    int years = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
    int months = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
    int days = cal2.get(Calendar.DAY_OF_MONTH) - cal1.get(Calendar.DAY_OF_MONTH);
    if (days < 0) {
        months--;
        days += cal1.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    if (months < 0) {
        years--;
        months += 12;
    }
    return new Term(years, months, days);
}
--------------------------------------------------------------------------------

<!--- // Create a java.util.GregorianCalendar Object // --->
<CFOBJECT ACTION="CREATE" TYPE="Java" CLASS="java.util.GregorianCalendar" NAME="cal1">
   <cfset ret1=cal1.init()>
<CFOBJECT ACTION="CREATE" TYPE="Java" CLASS="java.util.GregorianCalendar" NAME="cal2">
   <cfset ret2=cal2.init()>

<cfset dob = CreateODBCDateTime("04/15/1968")>
<cfset dt2 = CreateODBCDateTime("04/15/2000")>
<cfset aaa = cal1.getTimeInMillis()>

<cfscript>
function edDiff(dtA, dtB){
gc=createObject("java","java.util.GregorianCalendar");
gc.setTime(dtA);
gc2=createObject("java","java.util.GregorianCalendar");
gc2.setTime(dtB);

myyears=gc2.get(gc2.YEAR) - gc.get(gc.YEAR);
}            
</cfscript>

<cfoutput>
c: #edDiff(dob, dt2)#
</cfoutput>

==================================================

The cf code is just for demonstration and testing.

The problem I'm having is with this line of java:

         cal1.setTimeInMillis(from.getTime());

The getTime() function refers to the input prameters of the method.

this doesn't seem to be working for me. I'm wondering if dtA and dtB should be put into a gregorianCalendar object before they can be used in the getTime and setTimeInMilis function.

Any advice would be appreciated.



ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FastEddie___
FastEddie___

ASKER

Thanks agx.
I was looking for a more precise age calculation. I was just subtracting one date from another and using the modulus operator to handle the months. That was not accuratley giving an age for months.
From doing a bit of research I fond the java algorithm that seemed to do what I needed but it was created using the gregorianCalendar.

This algorighm is much more precise then the one I was using.
Thanks again!!!
Thank you.
You're welcome.  

>> I was looking for a more precise age calculation

Yep.  You can always go the java route if CF can't do what you need .. but it usually can ;-)