Skip to main content
Inspiring
January 10, 2022
Answered

CF2021 Issue with Java Date Object

  • January 10, 2022
  • 2 replies
  • 2447 views

Here's another issue I'm running into with our new CF2021 instance. This function worked fine with CF2016 using Java 11

 

    public date function convertEpochToODBCDate( required numeric ts ){
	var objTZ = createObject( "java", "java.util.TimeZone" );
	var objDF = createObject( "java", "java.text.SimpleDateFormat" );
	var objDate = createObject( "java", "java.util.Date" );
        var dte = objDate.init( JavaCast("long",arguments.ts) * 1000 );
	var tz = objTZ.getTimeZone( JavaCast("string", 'UTC') );
	var outputFormat = objDF.init( javaCast( "string", "yyyy-MM-dd HH:mm:ss" ) );
	    outputFormat.setTimeZone(tz);
        return CreateODBCDateTime( outputFormat.format(dte.getTime()) );
    }

 

 

Here's the stack trace:

DetailEither there are no methods with the specified method name and argument types or the init method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 2 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.
MessageThe init method was not found.
MethodNameinit
StackTracecoldfusion.runtime.java.MethodSelectionException: The init method was not found. at coldfusion.runtime.java.ObjectHandler.findConstructorUsingCFMLRules(ObjectHandler.java:496) at coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:222) at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:89) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:4254) at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:4217) 
    This topic has been closed for replies.
    Correct answer BKBK
     

    You can pass any epoch value (e.g. 1646874000) - ...


    By @--jojo--

     

    But beware. ColdFusion is weakly typed. Under the bonnet, it may make a funky conversion like 1.646874E+012 for 1646874000 * 1000.


    Yet another solution:

     public date function convertEpochToODBCDate( required numeric ts ){
     	...
            // Use BigDecimal to prevent ColdFusion from automatically  
            // converting 1646874000000 to 1.646874E+012
    	 var numberAsLong=createobject("java","java.math.BigDecimal").init(arguments.ts*1000).longValue();
    	var dte = objDate.init( numberAsLong );
            ...
     }

    2 replies

    Charlie Arehart
    Community Expert
    Community Expert
    January 10, 2022

    Please output and tell us the value of your arguments.ts value. The root cause of the error is likely in what's being passed in, rather the init itself. Indeed, you could also try to output or just refer to the JavaCast("long",arguments.ts), alone, to see if THAT is the actual error on that line.

     

    Someone might guess this could have something with the change in how cf 2021 handles date formats with D (vs d), but you don't show doing that in this code. And you're passing in a NUMBER in that ts argument, not a date.

     

    Let us know what you find. 

    /Charlie (troubleshooter, carehart. org)
    --jojo--Author
    Inspiring
    January 10, 2022

    You can pass any epoch value (e.g. 1646874000) - unfortunately cffidle won't let you instantiate java objects. 

    BKBK
    Community Expert
    Community Expert
    January 11, 2022
     

    You can pass any epoch value (e.g. 1646874000) - ...


    By @--jojo--

     

    But beware. ColdFusion is weakly typed. Under the bonnet, it may make a funky conversion like 1.646874E+012 for 1646874000 * 1000.

    --jojo--Author
    Inspiring
    January 10, 2022

    And it's specifically failing on 

    objDate.init( JavaCast("long",arguments.ts) * 1000 );

     

    Inspiring
    January 10, 2022

    I can't explain why CF2016 and CF2021 would behave differently if both are using Java 11, but my guess would be that by multiplying the casted long value by 1000, it's probably getting converted back to a CF integer, and when that's passed in the constructor call, there are no methods that accept an integer. Multiply by 1000 before JavaCast-ing, see if that works.

     

    var dte = objDate.init( JavaCast("long",arguments.ts * 1000));

     

    --jojo--Author
    Inspiring
    January 10, 2022

    It's a good thought @TheRealMC - however the error is indicating that the Java Date class does not have an init() method, not that the value is incorrect. With that said, I did what you suggested and got the same error - The init method was not found.