mani
mani

Reputation: 37

How to find the difference between two timestamp values in mule?

I am getting the timestamp value in the format "dd-MM-yyyy hh:mm:ss.SSSSSS" eg: "2016-05-22 01:02:36.909093". I need to calculate the difference between the two values of the same format mentioned above. Could anyone help to overcome this.

Upvotes: 0

Views: 3372

Answers (4)

Craig Bayley
Craig Bayley

Reputation: 81

You can generate a period using dataweave directly. If your payload was:

{
"date1":"21-07-2016 10:00:00.000000",
"date2":"22-07-2016 11:11:11.111111"
}

Then this dataweave gives your period:

%dw 1.0
%output application/json
%type fdate = :localdatetime {format: "dd-MM-yyyy HH:mm:ss.SSSSSS"}
---
{
period: 
payload.date1 as :fdate - payload.date2 as :fdate
}

Which will look like this as output:

{
  "period": "PT25H11M11.111111S"
}

Obviously you can then refactor to a different format or output target.

Upvotes: 1

duddukurimd
duddukurimd

Reputation: 30

We need to go for a groovy to check with that, below is the code that can help you

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
import java.util.Date;
import java.text.SimpleDateFormat;
format = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
                
if(flowVars.lastRunDateTime==null||flowVars.lastRunDateTime=='0')
flowVars.lastRunDateTime=flowVars.currentRunDateTime;

log.info("last run date time"+flowVars.lastRunDateTime);
log.info("current time"+flowVars.currentRunDateTime);

               date1=flowVars.lastRunDateTime;
               date2=flowVars.currentRunDateTime;
       
              
                   d1 = null;
                      d2 = null;
                      d3 = null;
                     
                     d1 = format.parse(date1);
                     d2 = format.parse(date2);
                     
                     diff=d2.getTime()-d1.getTime(); 
                      
                      t=diff/60000;
                     
                     log.info(t + " total Minutes ");                           
                                                   
                   if(t>30)
                   {
                     log.info("initial Time"+d1 );
                     calendar = Calendar.getInstance();
                     calendar.setTime(d1);
                     calendar.add(Calendar.MINUTE, ${time.gap});
                     d3= calendar.getTime();
                     log.info("Final Time"+d3);
                     flowVars.lastRunDateTime=format.format(d1);
                     flowVars.currentRunDateTime=format.format(d3);
                     log.info("last run date time"+flowVars.lastRunDateTime);
                     log.info("current time"+flowVars.currentRunDateTime);
                   }                     
                    
              
flowVars.put('IM_START_DATE', flowVars.lastRunDateTime.substring(0,10));
flowVars.put('IM_START_TIME' , flowVars.lastRunDateTime.substring(10,16) + '01');
flowVars.put('IM_END_DATE' , flowVars.currentRunDateTime.substring(0,10));
flowVars.put('IM_END_TIME' , flowVars.currentRunDateTime.substring(10,16) +'00');

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338775

I don't know about Mule, but on the Java side it is easy.

java.time

Assuming your input string is in UTC, parse it as an Instant after replacing that SPACE in the middle by a T.

String input = "2016-05-22 01:02:36.909093".replace( " " , "T" );
Instant instant = Instant.parse( input );

Instant now = Instant.now();

Duration duration = Duration.between( instant , now );

You can ask the Duration for its length as a total number of hours or total of days or a total of minutes etc.

Oddly, in Java 8 the Duration class is lacking convenient methods for interrogating the parts such as hours or minutes. In Java 9 this lacking is remedied by “getPart” methods.

The Duration class can parse and generate strings in the standard ISO 8601 format. For example, an hour and a half is PT1H30M.

Upvotes: 1

Alex Fernandez
Alex Fernandez

Reputation: 1942

You can use java for that and use Joda date library.

Upvotes: 0

Related Questions