如何找到mule中两个时间戳值之间的差异?

时间:2016-07-21 06:29:13

标签: timestamp mule difference

我以“dd-MM-yyyy hh:mm:ss.SSSSSS”格式获取时间戳值,例如:“2016-05-22 01:02:36.909093”。我需要计算上面提到的相同格式的两个值之间的差异。任何人都可以帮助克服这一点。

4 个答案:

答案 0 :(得分:1)

我不了解Mule,但在Java方面它很容易。

java.time

假设您的输入字符串是UTC格式,请在中间用Instant替换该空格后将其解析为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 );

您可以将Duration的长度视为总小时数或总天数或总分钟数等。

奇怪的是,在Java 8中,Duration类缺少用于查询诸如小时或分钟等部分的便捷方法。在Java 9中,缺少这种方法可以通过“getPart”方法来解决。

Duration类可以解析并生成标准ISO 8601格式的字符串。例如,一个半小时是PT1H30M

答案 1 :(得分:1)

您可以直接使用dataweave生成句点。如果你的有效载荷是:

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

然后这个数据编织给出了你的期间:

%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
}

这将是输出:

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

显然,您可以重构为不同的格式或输出目标。

答案 2 :(得分:0)

您可以使用java并使用Joda日期库。

答案 3 :(得分:0)

我们需要一个groovy来检查,下面是可以帮助你的代码

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');