Example usage for org.joda.time DateTime getDayOfWeek

List of usage examples for org.joda.time DateTime getDayOfWeek

Introduction

In this page you can find the example usage for org.joda.time DateTime getDayOfWeek.

Prototype

public int getDayOfWeek() 

Source Link

Document

Get the day of week field value.

Usage

From source file:module.mission.domain.Mission.java

License:Open Source License

protected boolean discountLunchDay(final DateTime dateTime) {
    final int dayOfWeek = dateTime.getDayOfWeek();
    return dayOfWeek != DateTimeConstants.SATURDAY && dayOfWeek != DateTimeConstants.SUNDAY
            && !isHoliday(dateTime);
}

From source file:net.sf.jasperreports.functions.standard.DateTimeFunctions.java

License:Open Source License

/**
 * Returns a date a number of workdays away. Saturday and Sundays are not considered working days.
 *///from ww  w.j  a v a 2  s. c o m
@Function("WORKDAY")
@FunctionParameters({ @FunctionParameter("dateObject"), @FunctionParameter("workdays") })
public Date WORKDAY(Object dateObject, Integer workdays) {
    Date convertedDate = convertDateObject(dateObject);
    if (convertedDate == null) {
        logCannotConvertToDate();
        return null;
    } else {
        DateTime cursorDT = new DateTime(convertedDate);
        int remainingDays = workdays;
        while (remainingDays > 0) {
            int dayOfWeek = cursorDT.getDayOfWeek();
            if (!(dayOfWeek == DateTimeConstants.SATURDAY || dayOfWeek == DateTimeConstants.SUNDAY)) {
                // Decrement remaining days only when it is not Saturday or Sunday
                remainingDays--;
            }
            cursorDT = dayOfWeek == DateTimeConstants.FRIDAY ? cursorDT.plusDays(3) : cursorDT.plusDays(1);
        }
        return cursorDT.toDate();
    }
}

From source file:net.sf.jasperreports.functions.standard.DateTimeFunctions.java

License:Open Source License

/**
 * Returns the number of working days between two dates (inclusive). Saturday and Sunday are not considered working days.
 *//*from   w  w w.  j a  va 2  s  .c  o m*/
@Function("NETWORKDAYS")
@FunctionParameters({ @FunctionParameter("startDate"), @FunctionParameter("endDate") })
public Integer NETWORKDAYS(Object startDate, Object endDate) {
    Date startDateObj = convertDateObject(startDate);
    if (startDateObj == null) {
        logCannotConvertToDate();
        return null;
    }
    Date endDateObj = convertDateObject(endDate);
    if (endDateObj == null) {
        logCannotConvertToDate();
        return null;
    } else {
        DateTime cursorDateTime = new DateTime(startDateObj);
        DateTime endDateTime = new DateTime(endDateObj);
        int workingDays = 0;
        if (cursorDateTime.isAfter(endDateTime)) {
            // Swap data information
            DateTime tmp = cursorDateTime;
            cursorDateTime = endDateTime;
            endDateTime = tmp;
        }
        while (Days.daysBetween(cursorDateTime, endDateTime).getDays() > 0) {
            int dayOfWeek = cursorDateTime.getDayOfWeek();
            if (!(dayOfWeek == DateTimeConstants.SATURDAY || dayOfWeek == DateTimeConstants.SUNDAY)) {
                workingDays++;
            }
            cursorDateTime = cursorDateTime.plusDays(1);
        }
        return workingDays;
    }
}

From source file:net.sourceforge.fenixedu.domain.space.EventSpaceOccupation.java

License:Open Source License

protected DateTime getInstant(boolean firstInstant, YearMonthDay begin, final YearMonthDay end,
        final HourMinuteSecond beginTime, final HourMinuteSecond endTime, final FrequencyType frequency,
        final DiaSemana diaSemana, final Boolean dailyFrequencyMarkSaturday,
        final Boolean dailyFrequencyMarkSunday) {

    DateTime instantResult = null;//from  w  w  w  . j a  v  a 2 s. com
    begin = getBeginDateInSpecificWeekDay(diaSemana, begin);

    if (frequency == null) {
        if (!begin.isAfter(end)) {
            if (firstInstant) {
                return begin.toDateTime(new TimeOfDay(beginTime.getHour(), beginTime.getMinuteOfHour(), 0, 0));
            } else {
                return end.toDateTime(new TimeOfDay(endTime.getHour(), endTime.getMinuteOfHour(), 0, 0));
            }
        }
    } else {
        int numberOfDaysToSum = frequency.getNumberOfDays();
        while (true) {
            if (begin.isAfter(end)) {
                break;
            }

            DateTime intervalEnd = begin
                    .toDateTime(new TimeOfDay(endTime.getHour(), endTime.getMinuteOfHour(), 0, 0));
            if (!frequency.equals(FrequencyType.DAILY) || ((dailyFrequencyMarkSaturday
                    || intervalEnd.getDayOfWeek() != SATURDAY_IN_JODA_TIME)
                    && (dailyFrequencyMarkSunday || intervalEnd.getDayOfWeek() != SUNDAY_IN_JODA_TIME))) {

                if (firstInstant) {
                    return begin
                            .toDateTime(new TimeOfDay(beginTime.getHour(), beginTime.getMinuteOfHour(), 0, 0));
                } else {
                    instantResult = intervalEnd;
                }
            }
            begin = begin.plusDays(numberOfDaysToSum);
        }
    }
    return instantResult;
}

From source file:net.sourceforge.fenixedu.util.DiaSemana.java

License:Open Source License

public static int getDiaSemana(DateTime dateTime) {
    return dateTime.getDayOfWeek() == 7 ? 1 : dateTime.getDayOfWeek() + 1;
}

From source file:net.sourceforge.fenixedu.util.DiaSemana.java

License:Open Source License

public static int getDiaSemana(YearMonthDay date) {
    DateTime dateTime = date.toDateTimeAtMidnight();
    return dateTime.getDayOfWeek() == 7 ? 1 : dateTime.getDayOfWeek() + 1;
}

From source file:net.sourceforge.fenixedu.util.renderer.GanttDiagram.java

License:Open Source License

private void calculateFirstAndLastInstantInWeeklyMode(YearMonthDay begin) {
    if (begin == null) {
        throw new IllegalArgumentException();
    }/*from   ww w. j a  v  a 2 s .c  om*/
    DateTime beginDateTime = begin.toDateTimeAtMidnight();
    beginDateTime = (beginDateTime.getDayOfWeek() != 1) ? beginDateTime.withDayOfWeek(1) : beginDateTime;
    setFirstInstant(beginDateTime);
    setLastInstant(beginDateTime.plusDays(6));
}

From source file:net.sourceforge.fenixedu.webServices.jersey.api.FenixAPICanteen.java

License:Open Source License

public static String get(String daySearch) {

    String locale = I18N.getLocale().toString().replace("_", "-");
    if (canteenInfo == null || canteenInfo.isJsonNull() || oldInformation()) {

        String canteenUrl = FenixConfigurationManager.getConfiguration().getFenixApiCanteenUrl();
        try {//from   w ww  .  j a v  a  2  s  .  c  o m
            Response response = HTTP_CLIENT.target(canteenUrl).request(MediaType.APPLICATION_JSON)
                    .header("Authorization", getServiceAuth()).get();

            if (response.getStatus() == 200) {
                JsonParser parser = new JsonParser();
                canteenInfo = (JsonObject) parser.parse(response.readEntity(String.class));
                day = new DateTime();
            } else {
                return new JsonObject().toString();
            }
        } catch (ProcessingException e) {
            e.printStackTrace();
            return new JsonObject().toString();
        }
    }

    JsonArray jsonArrayWithLang = canteenInfo.getAsJsonArray(locale);

    DateTime dayToCompareStart;
    DateTime dayToCompareEnd;

    DateTime dateTime = DateTime.parse(daySearch, DateTimeFormat.forPattern(datePattern));
    int dayOfWeek = dateTime.getDayOfWeek();
    if (dayOfWeek != 7) {
        dayToCompareStart = dateTime.minusDays(dayOfWeek);
        dayToCompareEnd = dateTime.plusDays(7 - dayOfWeek);
    } else {
        dayToCompareStart = dateTime;
        dayToCompareEnd = dateTime.plusDays(7);
    }

    JsonArray jsonResult = new JsonArray();
    for (JsonElement jObj : jsonArrayWithLang) {

        DateTime dateToCompare = DateTime.parse(((JsonObject) jObj).get("day").getAsString(),
                DateTimeFormat.forPattern(datePattern));

        if (dateToCompare.isAfter(dayToCompareStart) && dateToCompare.isBefore(dayToCompareEnd)) {
            jsonResult.add(jObj);
        }
    }
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    return gson.toJson(jsonResult);

}

From source file:net.tourbook.ui.views.calendar.CalendarGraph.java

License:Open Source License

private void drawCalendar(GC gc) {

    final int dayLabelXOffset = 1;

    final int XX = getSize().x;
    final int YY = getSize().y;

    //      System.out.println(_graphClean ? "clean!" : "NOT clean!");
    //      System.out.println(_highlightChanged ? "HL changed!" : "HL NOT changed");
    //      System.out.println("-----------");

    _fontHeight = gc.getFontMetrics().getHeight();

    if (_graphClean && _image != null) {

        final GC oldGc = gc;
        _highlight = new Image(getDisplay(), XX, YY);
        gc = new GC(_highlight);
        gc.drawImage(_image, 0, 0);//from  w w w  .  ja  v  a2  s. c o m

        drawSelection(gc);

        if (_highlightChanged) {
            drawHighLight(gc);
            _highlightChanged = false;
        }

        gc.dispose();
        oldGc.drawImage(_highlight, 0, 0);
        _highlight.dispose();
        return;
    }

    if (_scrollDebug) {
        System.out.println("Drawing year: " + _dt.getYear() + " week: " + _dt.getWeekOfWeekyear()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    if (_image != null && !_image.isDisposed()) {
        _image.dispose();
    }

    DateTime date = new DateTime(_dt);
    _image = new Image(getDisplay(), XX, YY);

    // update month/year dropdown box
    // look at the 1st day of the week after the first day displayed because if we go to
    // a specific month we ensure that the first day of the month is displayed in
    // the first line, meaning the first day in calendar normally contains a day
    // of the *previous* month
    if (_calendarYearMonthContributor.getSelectedYear() != date.plusDays(7).getYear()) {
        _calendarYearMonthContributor.selectYear(date.getYear());
    }
    if (_calendarYearMonthContributor.getSelectedMonth() != date.plusDays(7).getMonthOfYear()) {
        _calendarYearMonthContributor.selectMonth(date.getMonthOfYear());
    }

    final GC oldGc = gc;
    gc = new GC(_image);

    _refTextExtent = gc.stringExtent(_refText);
    final boolean oldLayout = _tinyLayout;
    _tinyLayout = (_refTextExtent.x > XX / 9); // getNumOfWeeks needs the _tinuLayout set

    if (oldLayout != _tinyLayout) { // the layout style changed, try to restore weeks and make selection visible
        if (_tinyLayout) {
            _dt_normal = _dt;
            _dt = _dt_tiny;
        } else {
            _dt_tiny = _dt;
            _dt = _dt_normal;
        }
        scrollBarUpdate();
        if (_selectedItem.id > 0) {
            switch (_selectedItem.type) {
            case DAY:
                gotoDate(new DateTime(0).plusDays(_selectedItem.id.intValue()));
                return;
            case TOUR:
                gotoTourId(_selectedItem.id);
                return;
            }
        }
    }

    final int numCols = 9; // one col left and right of the week + 7 week days
    final int numRows = getNumOfWeeks(); // number of weeks per month displayed (make sure _tinyLayout is already defined!)

    final Color alternate = _colorCache.getColor(0xf0f0f0);

    _tourFocus = new ArrayList<ObjectLocation>();
    _dayFocus = new ArrayList<ObjectLocation>();

    CalendarTourData[] data;

    final Font normalFont = gc.getFont();
    final FontData fd[] = normalFont.getFontData();
    fd[0].setStyle(SWT.BOLD);
    final Font boldFont = new Font(_display, fd[0]);

    final Rectangle area = getClientArea();
    gc.setBackground(_white);
    gc.setForeground(_black);
    gc.fillRectangle(area);

    final float dY = (float) YY / (float) numRows;
    float dX = (float) XX / (float) numCols;

    // keep the summary column at a minimal width and hide it completely if height goes blow usable value
    final int minSummaryWidth = _refTextExtent.x;
    final int minSummaryHeigth = (_refTextExtent.y * 2) / 3;
    int summaryWidth = 0;
    if (dY > minSummaryHeigth) {
        if (dX < minSummaryWidth) {
            summaryWidth = minSummaryWidth;
        } else {
            summaryWidth = (int) dX;
        }
    }

    final int minInfoWidth = _refTextExtent.x / 2;
    final int minInfoHeigth = (_refTextExtent.y / 3);
    int infoWidth = 0;
    if (dY > minInfoHeigth) {
        if (dX < minInfoWidth) {
            infoWidth = minInfoWidth;
        } else {
            infoWidth = (int) dX;
        }
    }

    dX = (float) (XX - summaryWidth - infoWidth) / (numCols - 2);

    _calendarAllDaysRectangle = new Rectangle(infoWidth, 0, (int) (7 * dX), YY);
    //      _calendarFirstWeekRectangle = new Rectangle(infoWidth, 0, (int) (7 * dX), (int) dY);
    //      _calendarLastWeekRectangle = new Rectangle( infoWidth, (int) ((getNumOfWeeks() - 1) * dY), (int) (7 * dX), (int) dY);

    // first draw the horizontal lines
    gc.setBackground(_white);
    gc.setForeground(_gray);
    for (int i = 0; i <= numRows; i++) {
        gc.drawLine(0, (int) (i * dY), XX, (int) (i * dY));
    }

    //      final Rectangle selectedRec = null;
    //      final CalendarTourData selectedTour = null;
    //      final boolean doSelection = false;

    final long todayDayId = (new Day(new DateTime())).dayId;

    gc.setFont(boldFont);
    // a rough guess about the max size of the label
    final Point[] headerSizes = { gc.stringExtent("22. May 99"), //$NON-NLS-1$
            gc.stringExtent("22. May"), //$NON-NLS-1$
            gc.stringExtent("22") }; //$NON-NLS-1$
    gc.setFont(normalFont);

    final String[] headerFormats = { "dd. MMM yy", "dd. MMM", "dd" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String headerFormat = UI.EMPTY_STRING;

    // Find a format for the day header which fits into the rectangle available;
    int g = 0;
    while (g < headerSizes.length && headerSizes[g].x > (dX - dayLabelXOffset)) {
        g++;
    }
    g = Math.min(g, headerSizes.length - 1); // if the cell is smaller than the shortest format (no index 'g' was found) we use the shortest format and relay on clipping
    // if (headerSizes[g].y < dY) {
    //    headerFormat = headerFormats[g];
    // }
    headerFormat = headerFormats[g];
    final int dayLabelWidht = headerSizes[g].x;
    int dayLabelHeight = headerSizes[g].y;

    // _tinyLayout = (refTextExtent.x > dX || refTextExtent.y > dY - dayLabelHeight) ? true : false;

    DateTime weekDate;
    long dayId = (new Day(date)).dayId; // we use simple ids

    // Weeks
    for (int i = 0; i < numRows; i++) {

        final int Y1 = (int) (i * dY);
        final int Y2 = (int) ((i + 1) * dY);

        // Days per week
        Rectangle dayRec = null;
        weekDate = date; // save the first day of this week as a pointer to this week

        if (infoWidth > 0) {
            final Rectangle infoRec = new Rectangle(0, Y1, infoWidth, (Y2 - Y1));
            drawWeekInfo(gc, date, infoRec);
        }

        for (int j = 0; j < 7; j++) {
            final int X1 = infoWidth + (int) (j * dX);
            final int X2 = infoWidth + (int) ((j + 1) * dX);
            //            final Rectangle dayRec = new Rectangle(X1, Y1, (X2 - X1), (Y2 - Y1));
            dayRec = new Rectangle(X1, Y1, (X2 - X1), (Y2 - Y1));
            final Day day = new Day(dayId);
            _dayFocus.add(new ObjectLocation(dayRec, dayId, day));
            dayId = day.dayId + 1;
            final int weekDay = date.getDayOfWeek();

            gc.setBackground(_white);

            // Day background rectangle
            if ((date.getMonthOfYear() % 2) == 1) {
                gc.setBackground(alternate);
                gc.fillRectangle(dayRec.x, dayRec.y + 1, dayRec.width, dayRec.height - 1);
            }

            data = _dataProvider.getCalendarDayData(date.getYear(), date.getMonthOfYear(),
                    date.getDayOfMonth());

            // Day header box
            if (!_tinyLayout) {
                gc.setForeground(_gray);
                gc.fillGradientRectangle(X1, Y1, dayRec.width + 1, dayLabelHeight, true); // no clue why I've to add 1 to the width, looks like a bug on Linux and does not hurt as we overwrite with the vertial line at the end anyway

                // Day header label
                gc.setFont(boldFont);
                if (day.dayId == todayDayId) {
                    gc.setForeground(_blue);
                } else if (weekDay == DateTimeConstants.SATURDAY || weekDay == DateTimeConstants.SUNDAY) {
                    gc.setForeground(_red);
                } else {
                    gc.setForeground(_darkGray);
                }
                gc.setClipping(X1, Y1, dayRec.width, dayLabelHeight); // this clipping should only kick in if shortest label format is still longer than the cell width
                gc.drawText(date.toString(headerFormat), X2 - dayLabelWidht - dayLabelXOffset, Y1, true);
                gc.setFont(normalFont);
                gc.setClipping(_nullRec);

            } else {
                dayLabelHeight = 0;
            }

            drawDayTours(gc, data, new Rectangle(dayRec.x, dayRec.y + dayLabelHeight, dayRec.width,
                    dayRec.height - dayLabelHeight));

            if (_tinyLayout && _showDayNumberInTinyView) {
                if (day.dayId == todayDayId) {
                    gc.setForeground(_blue);
                } else if (weekDay == DateTimeConstants.SATURDAY || weekDay == DateTimeConstants.SUNDAY) {
                    gc.setForeground(_red);
                } else {
                    gc.setForeground(_darkGray);
                }
                gc.setAlpha(0x50);
                gc.setFont(boldFont);
                gc.setClipping(dayRec);
                gc.drawText(date.toString(headerFormat), X2 - dayLabelWidht - dayLabelXOffset, Y1, true);
                gc.setFont(normalFont);
                gc.setClipping(_nullRec);
                gc.setAlpha(0xFF);
            }

            date = date.plusDays(1);
        }

        if (summaryWidth > 0) {
            final int X1 = infoWidth + (int) (7 * dX);
            final int X2 = X1 + summaryWidth;
            final Rectangle weekRec = new Rectangle(X1, Y1, (X2 - X1), (Y2 - Y1));
            final CalendarTourData weekSummary = _dataProvider.getCalendarWeekSummaryData(weekDate.getYear(),
                    weekDate.getWeekOfWeekyear());
            if (weekSummary.numTours > 0) {
                drawWeekSummary(gc, weekSummary, weekRec);
            }
        }

    }
    gc.setFont(normalFont);

    // and finally the vertical lines
    gc.setForeground(_display.getSystemColor(SWT.COLOR_GRAY));
    for (int i = 0; i <= 7; i++) {
        gc.drawLine(infoWidth + (int) (i * dX), 0, infoWidth + (int) (i * dX), YY);
    }

    // draw the selection on top of our calendar graph image so we can reuse that image
    _highlight = new Image(getDisplay(), XX, YY);
    gc = new GC(_highlight);
    gc.drawImage(_image, 0, 0);
    drawSelection(gc);
    oldGc.drawImage(_highlight, 0, 0);
    _highlight.dispose();

    boldFont.dispose();
    oldGc.dispose();
    gc.dispose();

    _graphClean = true;

}

From source file:netload.controller.ScrapeController.java

License:Open Source License

public ArrayList<Day> getWeekList() throws IOException, ParseException {
    List<Day> allDays = getAllDays();
    ArrayList<Day> week = new ArrayList<>();

    for (Day day : allDays) {
        DateTime dayTime = new DateTime(day.getDatum());
        week.add(day);/*  w ww .  ja va 2  s. c o m*/
        if (dayTime.getDayOfWeek() == 7) {
            break;
        }
    }

    return week;
}