Java Calendar Day toCountWorkDays(GregorianCalendar start, GregorianCalendar end)

Here you can find the source of toCountWorkDays(GregorianCalendar start, GregorianCalendar end)

Description

to Count Work Days

License

Apache License

Declaration

public static long toCountWorkDays(GregorianCalendar start, GregorianCalendar end) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    private Calendar calendar;
    private static final String COLON = ":";
    private static final String ZERO = "0";

    public static long toCountWorkDays(GregorianCalendar start, GregorianCalendar end) {
        long result = 0;
        GregorianCalendar startDate = new GregorianCalendar();
        GregorianCalendar endDate = new GregorianCalendar();

        startDate.setTime(start.getTime());
        endDate.setTime(end.getTime());//from  w  w w  . ja  v  a 2s  .  c om
        if ((startDate.get(GregorianCalendar.DAY_OF_WEEK) % 7) <= 1) {
            startDate.add(GregorianCalendar.DATE, 2 - (startDate.get(GregorianCalendar.DAY_OF_WEEK) % 7));
        }
        if ((endDate.get(GregorianCalendar.DAY_OF_WEEK) % 7) <= 1) {
            endDate.add(GregorianCalendar.DATE, -1 - (endDate.get(GregorianCalendar.DAY_OF_WEEK) % 7));
        }
        long totaldays = toDayInterval(startDate, endDate);
        int s = endDate.get(GregorianCalendar.DAY_OF_WEEK) - startDate.get(GregorianCalendar.DAY_OF_WEEK);
        if (s < 0) {
            s += 5;
        }
        if (totaldays % 7 != 0)
            result = s + (totaldays / 7) * 5 + 1;
        else
            result = (totaldays / 7) * 5;

        if (result < 0) {
            result = 0;
        }
        return result;
    }

    public String getTime() {
        return getHour() + COLON + getMinute();
    }

    public static long toDayInterval(Date startDate, Date endDate) {
        return (endDate.getTime() - startDate.getTime()) / 86400000;
    }

    public static long toDayInterval(GregorianCalendar startDate, GregorianCalendar endDate) {
        return (endDate.getTimeInMillis() - startDate.getTimeInMillis()) / 86400000;

    }

    public int getHour() {
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public String getMinute() {
        int tempMinute = calendar.get(Calendar.MINUTE);
        return tempMinute < 10 ? ZERO + tempMinute : Integer.toString(tempMinute);
    }

    public long getTimeInMillis() {
        return calendar.getTimeInMillis();
    }
}

Related

  1. setStartTimeOfDay(Calendar calender)
  2. setToBeginningOfDay(Calendar cal)
  3. setToStartOfDay(Calendar start)
  4. setToStartTimeOfTheDay(Calendar c)
  5. startOfDay(Calendar calendar)
  6. todayCalendar()
  7. toEndOfDayCalendar(Date date)
  8. toNextWorkDay(Calendar cal)
  9. trimToDay(Calendar calendar)