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.



FastEddie___Asked:
Who is Participating?
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.

_agx_Commented:
Not ignore the question, but you shouldn't need java for basic date calculations.  Unless there's something extra you need .. ?

<cfset dob = ParseDateTime("04/16/1968")>
<cfset dt2 = ParseDateTime("04/15/2000")>
<cfset result = termDiff(dob, dt2)>
<cfdump var="#result#">

<cfscript>
function termDiff(d1, d2)
{
    var tempDate = arguments.d2;
    var result = structNew();
      if (dateCompare(d2, d1) eq -1) {
            tempDate = d1;
            d1 = d2;
            d2 = tempDate;
      }
    result.years = dateDiff("yyyy", arguments.d1, arguments.d2);
    arguments.d2 = dateAdd("yyyy", -result.years, arguments.d2);
    result.months = dateDiff("m", arguments.d1, arguments.d2);
    arguments.d2 = dateAdd("m", -result.months, arguments.d2);
    result.days = dateDiff("d", arguments.d1, arguments.d2);
    return result;
}
</cfscript>


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
FastEddie___Author Commented:
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!!!
FastEddie___Author Commented:
Thank you.
_agx_Commented:
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 ;-)
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
Java EE

From novice to tech pro — start learning today.