Example usage for org.joda.time DateMidnight get

List of usage examples for org.joda.time DateMidnight get

Introduction

In this page you can find the example usage for org.joda.time DateMidnight get.

Prototype

public int get(DateTimeFieldType type) 

Source Link

Document

Get the value of one of the fields of a datetime.

Usage

From source file:io.viewserver.core.BusinessDayCalculator.java

License:Apache License

public int getBusinessDay(ReadableDateTime start, ReadableDateTime end) {
    DateMidnight startMidnight = new DateMidnight(start);
    DateMidnight endMidnight = new DateMidnight(end);

    int weekdayStart = startMidnight.get(DateTimeFieldType.dayOfWeek());
    int weekdayEnd = endMidnight.get(DateTimeFieldType.dayOfWeek());

    if (weekdayStart == DateTimeConstants.SATURDAY) {
        startMidnight = startMidnight.plusDays(2);
        weekdayStart = DateTimeConstants.MONDAY;
    } else if (weekdayStart == DateTimeConstants.SUNDAY) {
        startMidnight = startMidnight.plusDays(1);
        weekdayStart = DateTimeConstants.MONDAY;
    }//from w w w.  j a  v a  2s .  c om

    if (weekdayEnd == DateTimeConstants.SATURDAY) {
        endMidnight = endMidnight.minusDays(1);
        weekdayEnd = DateTimeConstants.FRIDAY;
    } else if (weekdayEnd == DateTimeConstants.SUNDAY) {
        endMidnight = endMidnight.minusDays(2);
        weekdayEnd = DateTimeConstants.FRIDAY;
    }
    int days = Days.daysBetween(startMidnight, endMidnight).getDays();

    startMidnight = startMidnight.plusDays(DateTimeConstants.SATURDAY - weekdayStart);
    endMidnight = endMidnight.plusDays(DateTimeConstants.SATURDAY - weekdayEnd);
    int daysBetweenWeekends = (int) ((endMidnight.getMillis() - startMidnight.getMillis())
            / (24 * 60 * 60 * 1000));
    int weekendDays = daysBetweenWeekends * 2 / 7;
    days -= weekendDays;

    return days;
}