Set createdDate to another DateTime attribute resulting in a different date time value

0
Hi All,   I have a use case where consider there are 2 Objects - Object1 and object2, I'm setting the system 'createdDate' of Object1 to Object2's a DateTime attribute, say Object2CreatedDate - this attribute has set localize to 'No' in the domain model.  In the change object of microflow where this is updated, Object2CreatedDate -> set -> Object1/createdDate. There is no formatting or parsing happening there, simply setting the value. But the resulting date is almost a day off from the actual date.    How to set the system createdDate as it is without any differences in the value? Any sugesstions?   Thanks, Sridevi    
asked
1 answers
1

Hi Sridevi,

 

You should localize the attribute.

or else

use this java action just pass the created date of the obj1 and return the date in java action.

// This file was generated by Mendix Studio Pro.
//
// 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 myfirstmodule.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import java.math.BigDecimal;
import java.util.TimeZone;

public class Java_action extends CustomJavaAction<java.util.Date>
{
	private final java.util.Date date;

	public Java_action(
		IContext context,
		java.util.Date _date
	)
	{
		super(context);
		this.date = _date;
	}

	@java.lang.Override
	public java.util.Date executeAction() throws Exception
	{
		// BEGIN USER CODE
        TimeZone userTimeZone = getContext().getSession().getTimeZone();
        int offsetMillis = userTimeZone.getOffset(System.currentTimeMillis());
        BigDecimal offsetDecimal = new BigDecimal(offsetMillis);
        BigDecimal divisor = new BigDecimal(3600000);
        BigDecimal offsetHours = offsetDecimal.divide(divisor, 6, BigDecimal.ROUND_HALF_UP);

        // Add offsetHours (in milliseconds) to the input date
        long offsetMillisFromHours = offsetHours.multiply(new BigDecimal(3600000)).longValue();
        long newTime = this.date.getTime() + offsetMillisFromHours;
        return new java.util.Date(newTime);
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 * @return a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "Java_action";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

 

answered