cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate an average date between two dates??

Former Member
0 Kudos

Hello,

I have got two date fields in my webdynpro application. I am selecting one begin date from one field and an end date from another field and I need to calculate the middle date from these two. I will be calculating the year, month and quarter based on this middle date. I have written a code in which I am able to find difference in days between the two dates but am unable to get the same in date format. My code is:


long diff = wdContext.currentContextElement().getEnd_lc().getTime() - wdContext.currentContextElement().getBegin_date().getTime();

int daysdiff = (int)(diff /(1000*60*60*24*2))*2;

This code gives me the number of days. Please tell me how to complete my requirement.

Regards,

Gaurav

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Gaurav,

Use following code.

Date mdl=Date.valueOf("2008-01-01");//initialize the variable

long diff = wdContext.currentContextElement().getEnd_lc().getTime() - wdContext.currentContextElement().getBegin_date().getTime();

mdl.setTime(wdContext.currentContextElement().getBegin_date().getTime()+(diff/2));

This will set the middle date in variable mdl.

Regards

Deepak

Answers (2)

Answers (2)

Former Member
0 Kudos

Thanks a lot Deepak...

The code worked and Now aI am able to calculate the middle date.

Thanks again

Regards,

Gaurav

Former Member
0 Kudos

Use the GregorianCalendar class or any other Calendar class reflecting the calendar you want to calculate with:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html