Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to calculate Difference between two datetime excluding weekend

807601Jan 2 2008 — edited Jan 4 2008
Hi,

I'm not expert in java, but know bit.

I have to find difference between two datetime strings without weekends.

if user provides 02-Jan-2008 10:00:00 and 02-Jan-2008 10:30:00, my output should be 30mins.
similarly, for 02-Jan-2008 10:00:00 and 02-Jan-2008 11:30:00, output should be 90 mins.

but the result should not include weekend.
this is my first requirement.
My next hilarious job is, the time stamps within9:00am to 06:00pm (ie 09:00 to 18:00) should only taken for calculation. i'm bit afraid i could do it.

the code snippet that i'm working now is BSH (it doesnot accepts some java functions)

/* to find Weekends*/
long weekEnds(Calendar stCal, Calendar enCal)
{
enCal.add(enCal.DATE,1);
long count=0;
while(stCal.before(enCal)){
if (stCal.get(Calendar.DAY_OF_WEEK)==1 || stCal.get(Calendar.DAY_OF_WEEK)==7 ) count++;
stCal.add(stCal.DATE,1);
}
return count; //returns number of weekends between the provided dates
}


String dateDiff(String frDate, String toDate)
{
Calendar frCal = Calendar.getInstance();
Calendar toCal = Calendar.getInstance();
long weekEnd=0.0;

try{
Date d1=new Date(frDate);
Date d2=new Date(toDate);
if(d1.after(d2)) return "Date 1 cannot be Greater than Date 2";
frCal.setTime(d1);
toCal.setTime(d2);
long d1Ms=d1.getTime();
long d2Ms=d2.getTime();

weekEnd = (weekEnds(frCal,toCal))*(24*60); //converting to mins
return (Math.abs(((d2Ms-d1Ms)/(60*1000)) - weekEnd)).toString();
}catch(Exception e){return "Err";}
}


plz help me.. i was hardly dying for past 6 hrs with this small bit code.

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 1 2008
Added on Jan 2 2008
8 comments
2,688 views