cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between two timestamps - Message Mapping

openrico
Participant
0 Kudos

Hi Professionals,

i want to get the difference between two timestamps or datetimes in my message mapping.

I am not good at Java, so i dont know how to achive this in an UDF.

My time format is eather hh:mm:ss or yyyy-mm-dd hh:mm:ss

The result should be a float in hours.

Eg: i have 10:00:00 and 10:30:00 so its 30min -> 0,5 hours

I would appriciate if someone could provide me the code or some help.

thanks!
Best regards

Enrico

Accepted Solutions (1)

Accepted Solutions (1)

former_member186851
Active Contributor
0 Kudos

Hello,

Try the below UDF

String result = "0";

long time = 0;

//long time = timeMillis / 1000;

String hours = Integer.toString((int)(time / 3600));

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

try {

Date date1 = format.parse(time1);

Date date2 = format.parse(time2);

long diff = date2.getTime() - date1.getTime();

float f1 = diff;

float diffHours = f1 / (60 * 60 * 1000) % 24;

String Out= Float.toString(diffHours);

;result = Out;

} catch (Exception e) {

        e.printStackTrace();

}

return result;

Time 1 and Time2 are the inputs.

openrico
Participant
0 Kudos

Hi,

thank you for your answer. It is exactly what i need.

I had to import the package java.lang.* and java.text.* and now it is working

Best regards.

Enrico

former_member186851
Active Contributor
0 Kudos

Hi,

Yes..Sorry forgot to mention that

Glad that it worked.

Answers (0)