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();
}
}