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();
    GregorianCalendar cal2 = new GregorianCalendar();
    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) {
        days += cal1.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (months < 0) {
        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()>

function edDiff(dtA, dtB){

myyears=gc2.get(gc2.YEAR) - gc.get(gc.YEAR);

c: #edDiff(dob, dt2)#


The cf code is just for demonstration and testing.

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


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#">

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;

_agx_'s gravatar image


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___'s gravatar image


Thank you.
FastEddie___'s gravatar image


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


Your answer
[hide preview]

Follow this question

By Email:

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



Answers and Comments



Asked: 10/17/2010 03:20

Seen: 291 times

Last updated: 10/17/2010 03:18