Java Week Day getDaysExceptWeekend(String from, String to)

Here you can find the source of getDaysExceptWeekend(String from, String to)

Description

get Days Except Weekend

License

Open Source License

Declaration

public static int getDaysExceptWeekend(String from, String to) 

Method Source Code

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

Related

  1. getDayOfWeek(String date)
  2. getDayOfWeek(String year, String month, String day)
  3. getDayOfWeekAbbreviation(int day)
  4. getDayOfWeekAbreviated(Date d)
  5. getDayOfWeekShortString(Date date)
  6. getDayWithWeek(Date date)
  7. getENWeekDay()
  8. getFirstday_Lastday_Week()
  9. getFirstDayOfLastWeek(Date date)