1

I am beginner in android. I want to calculate "Days" between two dates with time excluding weekends.

For this, I wrote below code but according to given dates still there is no one day completed but I got No of Days as "1".

How can I solve this problem?

Please help me.

code:-

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.shared_layout);

    try{

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm a");

        //Current Date:-
        Date date1 = format.parse("13/06/2016 10:20 AM");

        //Present date:-    
        Date date2 = format.parse("14/06/2016 10:19 AM");

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);

        int numberOfDays = 0;

        while (cal1.before(cal2)) {
            if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
                    &&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
                numberOfDays++;
            }
            cal1.add(Calendar.DATE,1);
        }

        System.out.println(numberOfDays);

        System.out.println("No of Days is===>"+numberOfDays);

//            Toast.makeText(DateAndTimeDiffExceptWeeckEnds.this, "No of Days is===>"+numberOfDays,
//                    Toast.LENGTH_LONG).show();

    }catch (ParseException e){
        e.printStackTrace();
    }
}
Mansuro
  • 4,410
  • 4
  • 33
  • 74
Krish
  • 3,890
  • 10
  • 50
  • 96
  • try this it may be help to you http://stackoverflow.com/questions/37460807/age-calculate-when-user-select-dob-from-date-picker/37461014#37461014 – Abhishek Patel Jun 13 '16 at 05:08
  • No it's not help for me because i want o to calculate No of days b/w two dates with including time and excluding week ends – Krish Jun 13 '16 at 05:11
  • How it it become duplicate here my dought is How to calculating “Days” between two dates with time excluding week ends not weeck ends count – Krish Jun 13 '16 at 05:14
  • many questions like this answered on SO .. https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=calendar%20java%20findout%20week%20ends%20between%20two%20dates – Bharatesh Jun 13 '16 at 05:22

1 Answers1

0

This is your issue:

while (cal1.before(cal2))

True, cal2 is not 24 hours after cal1, but cal21 is before cal2, so the loop executes once, so 1 is added to numberOfDays. Instead, you'll want to actually check if cal1 + 24 hours (1 day) is before cal 2. If so, that constitutes a day (unless weekends). Otherwise, the loop will break.

So, you can simply call cal1.add(Calendar.DATE,1); just prior to the start of the while loop; the rest of the code stays the same.

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.shared_layout);

    try{

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm a");

        //Current Date:-
        Date date1 = format.parse("13/06/2016 10:20 AM");

        //Present date:-    
        Date date2 = format.parse("14/06/2016 10:19 AM");

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);

        int numberOfDays = 0;

        //NEW CODE: add 1 first, then increment numberOfDays if applicable
        cal1.add(Calendar.DATE,1);
        //END NEW CODE
        while (cal1.before(cal2)) {
            if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
                    &&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
                numberOfDays++;
            }
            cal1.add(Calendar.DATE,1);
        }

        System.out.println(numberOfDays);

        System.out.println("No of Days is===>"+numberOfDays);

//            Toast.makeText(DateAndTimeDiffExceptWeeckEnds.this, "No of Days is===>"+numberOfDays,
//                    Toast.LENGTH_LONG).show();

    }catch (ParseException e){
        e.printStackTrace();
    }
}

That said, probably a library out there that may do this more efficiently (which probably use an equation to factor out the weekend days based on distance between days, for example). But the above should get you going.

GoGoCarl
  • 2,431
  • 11
  • 16