cancel
Showing results for 
Search instead for 
Did you mean: 

Arithmetic operations with time

Former Member
0 Kudos

Hi all,

I want to do arithmetic operations with time data types. Like

d1,d2,d3 are of date types

long diff = d1.getTime()-d2.getTime()-d3.getTime();

long hours = diff/(60 * 60 * 1000) % 24);

long mins = diff / (60 * 1000) % 60;

long secs =  diff / 1000 % 60;

When i am entering time for d1 as 4:00:00, d2 as 9:00:00 and d3 as 1:00:00, i am getting hours & mins in negative values.

Can anyone suggest how to do this operation

Accepted Solutions (0)

Answers (1)

Answers (1)

govardan_raj
Contributor
0 Kudos

hi sahithi,

when d1 d2 and d3 are date types how can u enter or set 4:00:00 to it, which is time representation.

what exactly u are trying to do ...

and it is obvvious that d1-d2-d3 is 4:00:00 -9:00:00 -1:00:00 here u can see that 9 is greater and its sign is predominant according to this expression. hence diff is negative value. and then as usuall hours and secs are also in negative.

any how follow below code you can solve yoru issue i hope

String fromdate = "11/03/14 09:29:58";
   
String enddate = "11/03/14 09:33:43";

   
Custom date format
   
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");

   
Date d1 = null;
   
Date d2 = null;
   
try {
        d1
= format.parse(dateStart);
        d2
= format.parse(dateStop);
   
} catch (ParseException e) {
        e
.printStackTrace();
   
}

  

   
long diff = d2.getTime() - d1.getTime();
   
long diffSeconds = diff / 1000 % 60;  // for seconds
   
long diffMinutes = diff / (60 * 1000) % 60; // for minute
   
long diffHours = diff / (60 * 60 * 1000); // for hours
 

 

wdComponentAPI.getMessageManager().reportSuccess

("Seconds---> " + diffSeconds +"sec");
   

 

wdComponentAPI.getMessageManager().reportSuccess

("Minutes---> " + diffMinutes + " min.");
   

 

wdComponentAPI.getMessageManager().reportSuccess

("Hours--->" + diffHours + " hrs.");

Regards

Govardan