Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between dates considering weekends

Status
Not open for further replies.

saiza

Programmer
Apr 26, 2002
24
IN
hi..


I would like to know as to how i calculate the number of days between 2 dates considering that no weekend days(sat and sundays not to be included in this calculation).

preferably using the gregorianCalendar class
can soembody help out plzz... its urgent!!! ()
 
How's your french? A french colleague developped this this morning!
Code:
/** @return the number of open days between sysdate and     dateRecup*/
  public static int nbWorkdays(String dateRecup)
  {
    GregorianCalendar calendar = new GregorianCalendar();
    Date sysDate = new Date();
    calendar.setTime(sysDate);
    
    //get a list of french holidays...
    String valRetour[] = new Workdays().calc(calendar.get(Calendar.YEAR));

    SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
    String curDate = formatDate.format(sysDate);

    Date dateConvert = null;
    
    int joursOuvres = 0;

    while (!dateRecup.equals(curDate))
    {
      try {
       dateConvert = formatDate.parse(dateRecup);
       calendar.setTime(dateConvert);
      }
      catch (ParseException pe){
      }
      /*Ajout d'une journée à la date d'entrée*/
      dateConvert.setTime(dateConvert.getTime()+(24 * 3600 * 1000));
      /*Conversion de la date d'entrée en String*/
      dateRecup = formatDate.format(dateConvert);

      joursOuvres++;

      /*Soustraction d'une journée si dateRecup = samedi ou dimanche*/
      if (calendar.get(Calendar.DAY_OF_WEEK) == 7 
      || calendar.get(Calendar.DAY_OF_WEEK) == 1)           joursOuvres--;
      else
      {
        /*Soustraction d'une journée si dateRecup est férié*/
        for (int i = 0; i < valRetour.length; i++)
        {
          if(valRetour[i].toString().indexOf(dateRecup) != -1) joursOuvres--;
        }
      }
    }
    return joursOuvres;
  }
joursOuvres are work days...
 
I have been workign on that for sometime now...

Please find below my code....

public int removeWeekEnds(String plannedst, String plannedend)
{

if (plannedst.equals(&quot;&quot;)) return 0;
StringTokenizer plstT = new StringTokenizer(plannedst,&quot;/&quot;);
int plday =Integer.parseInt(plstT.nextToken());
int plmon =Integer.parseInt( plstT.nextToken());
int plyear =Integer.parseInt(plstT.nextToken());

Date plstDate = new Date(plyear+&quot;/&quot;+plmon+&quot;/&quot;+plday);
int pldayst = (int) ((plstDate.getTime()) / (1000 * 60 * 60 * 24));

if (plannedend.equals(&quot;&quot;)) return 0;
StringTokenizer pldnT = new StringTokenizer(plannedend,&quot;/&quot;);
int pldayen =Integer.parseInt(pldnT.nextToken());
int plmonen =Integer.parseInt(pldnT.nextToken());
int plyearen =Integer.parseInt(pldnT.nextToken());

Date plenDate = new Date(plyearen+&quot;/&quot;+plmonen+&quot;/&quot;+pldayen);
int pldayend = (int) ((plenDate.getTime()) / (1000 * 60 * 60 * 24));
esteffortint = (pldayend - pldayst)+1;
// This is the number of days between two days &quot;esteffortint&quot;
int x = esteffortint;
GregorianCalendar kc = new GregorianCalendar(plyear, plmon, plday);

for(int i=0; i<=x; i++)
{

plyear = kc.get(Calendar.YEAR);
plmon = kc.get(Calendar.MONTH);
plday = kc.get(Calendar.DATE);
int dayInWeek= kc.DAY_OF_WEEK;

if (dayInWeek ==1 || dayInWeek == 7)
{
esteffortint--;

}

kc.add(Calendar.DATE, 1);
kc.set(plyear,plmon,plday);



}



return esteffortint;

}


This is printing the wrong week of the day for me.... the number of days that it should return is 21 ...but as such the result i get is 22...

Can somebody plzz tell mewhere I am going wrong... i did try a lot of options.....
 
hey guys... This code works..... Thanx a lot for all the help daniel135

GregorianCalendar kc = new GregorianCalendar(plyear, plmon, plday);

kc.setLenient(true);

int day = kc.get(Calendar.DAY_OF_WEEK);

if (day == Calendar.SATURDAY || day == Calendar.SUNDAY)
{
esteffortint--;
}

plday = plday + 1;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top