3
StartDate:  2016-05-8 20:16:00;
EndDate:    2016-05-30 20:16:00;


     public int saturdaysundaycount(Date d1, Date d2) {
                Calendar c1 = Calendar.getInstance();
                c1.setTime(d1);

                Calendar c2 = Calendar.getInstance();
                c2.setTime(d2);

                int sundays = 0;
                int saturday = 0;

                while (c1.after(c2)) {
                    if (c2.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || c2.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
                        sundays++;
                    saturday++;
                    c2.add(Calendar.DATE, 1);
                    c2.add(Calendar.DATE, 1);
                }
                System.out.println(sundays);

                return saturday + sundays;
            }

In this function I am trying to get total count of Saturdays and Sundays between two dates. But when I pass the date I get zero as a result. Please point out the mistake and suggest corrections.

Mrunal Pagnis
  • 801
  • 1
  • 9
  • 26
Research Development
  • 884
  • 1
  • 19
  • 39

4 Answers4

9

It is not advisable to write full program but since you put effort, here is what seems to be working on my system and returning a value of 7.

public class CountWeekends {

    public static void main(String[] args){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        int count = 0;
        try {
            Date d1 = formatter.parse("2016-05-8 20:16:00");
            Date d2 = formatter.parse("2016-05-30 20:16:00");
            count = saturdaysundaycount(d1,d2);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Count of Sats & Sundays = "+count);
    }

    public static int saturdaysundaycount(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);

        int sundays = 0;
        int saturday = 0;

        while (! c1.after(c2)) {
            if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ){
                saturday++; 
            }
            if(c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
                sundays++;
            }

            c1.add(Calendar.DATE, 1);
        }

        System.out.println("Saturday Count = "+saturday);
        System.out.println("Sunday Count = "+sundays);
        return saturday + sundays;
    }

Logic: You need to keep increment start date by one day till it surpasses end date and keep checking day on start date.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
2

The problem is in your while, with this piece of code is working fine for me.

Check the endDate and startDate because I guess that you are sending it in the wrong order.

while (endDate.after(startDate)) {
  if (endDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ){
    sundays++;
  }else if (endDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
    saturday++;
  }

  endDate.add(Calendar.DATE, -1);
}
SCouto
  • 7,808
  • 5
  • 32
  • 49
0
public static int getNumberofSundays(String d1,String d2) throws Exception{ //object in Date form
    Date date1=getDate(d1);
    Date date2=getDate(d2);

    Calendar c1=Calendar.getInstance();
    c1.setTime(date1);
    Calendar c2=Calendar.getInstance();
    c2.setTime(date2);
    int sundays=0;
    while(c1.after(c2)){
        if(c2.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
            sundays++;
            c2.add(Calendar.DATE,1);
        }
    }
    System.out.println("number of days between 2 dates"+sundays);

    return sundays;
}
0

Your code does not loop through the days. Please try the following code. In the while loop it loops through all the days between the given fist date and last date. It does this by adding a day to c1 in every iteration until c1 is after c2. This gives number of Saturdays and Sundays between given dates including those two days.

public static int saturdaysundaycount(Date d1, Date d2) {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(d1);

    Calendar c2 = Calendar.getInstance();
    c2.setTime(d2);

    int sundays = 0;
    int saturdays = 0;

    while (!c1.after(c2)) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd-E");
        String formatted = format1.format(c1.getTime());
        System.out.println("Current Date C1 : " + formatted);

        if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            sundays++;
        } else if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            saturdays++;
        }
        c1.add(Calendar.DATE, 1);
    }

    System.out.println("Sundays : " + sundays);
    System.out.println("Saturdays : " + saturdays);

    return saturdays + sundays;
}
hetptis
  • 786
  • 1
  • 12
  • 23