Here you can find the source of getDaysExceptWeekend(String from, String to)
public static int getDaysExceptWeekend(String from, String to)
//package com.java2s; import java.util.Calendar; public class Main { public static int getDaysExceptWeekend(String from, String to) { if (from.compareTo(to) > 0) { String tmp = from;/*from w w w . j a v a 2 s .c o m*/ from = to; to = tmp; } Calendar begin = parseCalendar(from); Calendar end = parseCalendar(to); int sum = 0; while (!dateEquals(begin, end)) { if (!isWeekEnd(begin)) { sum++; } begin.add(Calendar.DATE, 1); } if (!isWeekEnd(end)) { sum++; } return sum; } public static Calendar parseCalendar(String s) { Calendar c = Calendar.getInstance(); try { int d = Integer.parseInt(s); int year = d / 10000; int month = (d % 10000) / 100; int day = d % 100; c.set(year, month - 1, day); } catch (Exception e) { return null; } return c; } public static boolean dateEquals(Calendar c1, Calendar c2) { return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) && c1.get(Calendar.DATE) == c2.get(Calendar.DATE); } public static boolean isWeekEnd(Calendar c) { int day = c.get(Calendar.DAY_OF_WEEK); if (day == Calendar.SUNDAY || day == Calendar.SATURDAY) { return true; } return false; } public static boolean isWeekEnd(String date) { return isWeekEnd(parseCalendar(date)); } }