Clicky

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.



asked 10/17/2010 03:20

FastEddie___'s gravatar image

FastEddie___ ♦♦


4 Answers:
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>


link
_agx_'s gravatar image

_agx_

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!!!

link
FastEddie___'s gravatar image

FastEddie___

Thank you.
link
FastEddie___'s gravatar image

FastEddie___

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 ;-)
link
_agx_'s gravatar image

_agx_

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×6
×32
×2
×141

Asked: 10/17/2010 03:20

Seen: 283 times

Last updated: 10/17/2010 03:18