Java Action issue with data type not matching the domain model

I have a basic java action that takes an input parameter of seconds as a float and converts it to a string. The issue is the java action parameters combine Float/Currency. When the java file is generated/edited by the Modeler 4.1.0, it's defining the input parameter as a double. Is there a way to ensure it is defined as a float? Contents of the java file are below. The field being passed in is a float as well in the domain model. // This file was generated by Mendix Business Modeler 4.0. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments. package loans.actions; import com.mendix.systemwideinterfaces.core.UserAction; /** */ public class SecondsToDuration extends UserAction<string> { private Double secsIn; public SecondsToDuration(Double secsIn) { super(); this.secsIn = secsIn; } @Override public String executeAction() throws Exception { // BEGIN USER CODE float elapsedTime =secsIn.floatValue(); String format = String.format("%%0%dd", 2); elapsedTime = elapsedTime / 1000; String seconds = String.format(format, elapsedTime % 60); String minutes = String.format(format, (elapsedTime % 3600) / 60); String hours = String.format(format, elapsedTime / 3600); String time = hours + ":" + minutes + ":" + seconds; return time; // return "00:00:00"; //throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented"); // END USER CODE } /** * Returns a string representation of this action */ @Override public String toString() { return "SecondsToDuration"; } // BEGIN EXTRA CODE // END EXTRA CODE }
2 answers

A double is a float with more precision so why would you want the Modeler to generate an actual float as input? Just use the double, you can do the same kind of stuff with it as with a float.

Btw, I'm not sure why you are using a float/double as a time input since you shouldn't need floating point precision (millisecond is usually the smallest unit, but are you using milliseconds here or always using full seconds? Since there IS a division by 1000 in there as well I'd say milliseconds), looks to me you could benefit a lot from

Additionally, keep in mind that once you converted some date value to a string, it is no longer adjustable for timezones.


Thanks. I was actually accruing total seconds spent on every task using secondsBetween the start date of the task and the end date and keeping a running total. The root error was a Java conversion error with the string format. I was able to get the string formatted correctly with the update code below.

// BEGIN USER CODE int x= secsIn.intValue(); int hours = x / 3600, remainder = x % 3600, minutes = remainder / 60, seconds = remainder % 60;

return ( (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds< 10 ? "0" : "") + seconds );