Example usage for org.joda.time DateTime plusDays

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

Introduction

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

Prototype

public DateTime plusDays(int days) 

Source Link

Document

Returns a copy of this datetime plus the specified number of days.

Usage

From source file:com.pacoapp.paco.shared.scheduling.NonESMSignalGenerator.java

License:Open Source License

private DateTime getNextScheduleDay(DateTime midnightTomorrow) {

    switch (schedule.getScheduleType()) {
    case Schedule.DAILY:
        return nextRepeatDaily(midnightTomorrow);

    case Schedule.WEEKDAY:
        int tomorrowDOW = midnightTomorrow.getDayOfWeek();
        if (tomorrowDOW > DateTimeConstants.FRIDAY) {
            return midnightTomorrow.plusDays(8 - tomorrowDOW);
        } else {//w  w  w  . j a v  a2 s .c  om
            return midnightTomorrow;
        }

    case Schedule.WEEKLY:
        int scheduleDays = schedule.getWeekDaysScheduled();
        if (scheduleDays == 0) {
            return null;
        }
        for (int i = 0; i < 8; i++) { // go at least to the same day next week.
            int midnightTomorrowDOW = midnightTomorrow.getDayOfWeek();
            Integer nowDowIndex = Schedule.DAYS_OF_WEEK[midnightTomorrowDOW == 7 ? 0 : midnightTomorrowDOW]; // joda is 1 based & counts Monday as first day of the week so Sunday is 7 instead of 0.
            if ((scheduleDays & nowDowIndex) == nowDowIndex) {
                return nextRepeatWeekly(midnightTomorrow);
            }
            midnightTomorrow = midnightTomorrow.plusDays(1);

        }
        throw new IllegalStateException("Cannot get to here. Weekly must repeat at least once a week");

    case Schedule.MONTHLY:
        if (schedule.getByDayOfMonth()) {
            int midnightDOM = midnightTomorrow.getDayOfMonth();
            int scheduledDOM = schedule.getDayOfMonth();
            if (midnightDOM == scheduledDOM) {
                return midnightTomorrow;
            } else if (midnightDOM > scheduledDOM) {
                MutableDateTime mutableDateTime = midnightTomorrow.plusMonths(1).toMutableDateTime();
                mutableDateTime.setDayOfMonth(scheduledDOM);
                return nextRepeatMonthly(mutableDateTime.toDateTime());
            } else {
                return nextRepeatMonthly(midnightTomorrow.plusDays(scheduledDOM - midnightDOM));
            }
        } else {
            Integer nthOfMonth = schedule.getNthOfMonth();
            Integer dow = getDOWFromIndexedValue(); // only one selection, so take log2 to get index of dow
            DateMidnight nthDowDate = getNthDOWOfMonth(midnightTomorrow, nthOfMonth, dow).toDateMidnight();
            DateTime returnDate = null;
            if (nthDowDate.equals(midnightTomorrow)) {
                returnDate = midnightTomorrow;
            } else if (nthDowDate.isAfter(midnightTomorrow)) {
                returnDate = nthDowDate.toDateTime();
            } else {
                returnDate = getNthDOWOfMonth(midnightTomorrow.plusMonths(1), nthOfMonth, dow).toDateTime();
            }
            return nextRepeatMonthly(returnDate);
        }
    default:
        throw new IllegalStateException("Schedule has an unknown type: " + schedule.getScheduleType());
    }
}

From source file:com.pandits.opensource.JodaTimeUtil.java

License:Open Source License

public Date advanceBy(Date date, int numOfDays) {
    DateTime dateTime = createDateTime(date);
    return dateTime.plusDays(numOfDays).toDate();
}

From source file:com.qcadoo.commons.dateTime.TimeRange.java

License:Open Source License

public Interval toInterval(final LocalDate date) {
    DateTime start = date.toDateTime(getFrom());
    DateTime end = date.toDateTime(getTo());
    if (startsDayBefore()) {
        end = end.plusDays(1);
    }/*from   w  ww  .  ja  v  a 2s  .c  o  m*/
    return new Interval(start, end);
}

From source file:com.qcadoo.mes.assignmentToShift.print.xls.AssignmentToShiftXlsHelper.java

License:Open Source License

public List<DateTime> getDaysBetweenGivenDates(final Entity entity) {
    List<DateTime> days = new LinkedList<DateTime>();

    DateTime dateFrom = new DateTime((Date) entity.getField(DATE_FROM));
    DateTime dateTo = new DateTime((Date) entity.getField(DATE_TO));

    DateTime nextDay = dateFrom;

    int numberOfDays = Days.daysBetween(dateFrom.toDateMidnight(), dateTo.toDateMidnight()).getDays();

    days.add(nextDay);/*www. j a  v  a2s . c  om*/

    int oneDay = 1;
    while (numberOfDays != 0) {
        nextDay = nextDay.plusDays(oneDay).toDateTime();
        days.add(nextDay);
        numberOfDays--;
    }

    return days;
}

From source file:com.qcadoo.mes.avgLaborCostCalcForOrder.AverageCostService.java

License:Open Source License

private BigDecimal getWorkedHoursOfWorker(final Entity shift, final DateTime dateOfDay) {
    BigDecimal hours = BigDecimal.ZERO;
    List<ShiftHour> workedHours = shiftsService.getHoursForShift(shift, dateOfDay.toDate(),
            dateOfDay.plusDays(1).toDate());
    for (ShiftHour shiftHour : workedHours) {
        DateTime dateFrom = new DateTime(shiftHour.getDateFrom());
        DateTime dateTo = new DateTime(shiftHour.getDateTo());
        Period p = new Period(dateFrom, dateTo);
        hours = hours.add(new BigDecimal(p.getHours()));
    }/*from   w  w  w  . ja va 2 s .  c  om*/
    return hours;
}

From source file:com.qcadoo.mes.avgLaborCostCalcForOrder.AverageCostService.java

License:Open Source License

private List<DateTime> getDaysBetweenGivenDates(final Date start, final Date finish) {
    List<DateTime> days = new LinkedList<DateTime>();
    DateTime startDate = new DateTime(start);
    DateTime finishDate = new DateTime(finish);

    DateTime nextDay = startDate;
    int numberOfDays = Days.daysBetween(startDate.toDateMidnight(), finishDate.toDateMidnight()).getDays();
    days.add(nextDay);//from  w  ww.  j av a  2s .  c  om

    int oneDay = 1;
    while (numberOfDays != 0) {
        nextDay = nextDay.plusDays(oneDay).toDateTime();
        days.add(nextDay);
        numberOfDays--;
    }
    return days;
}

From source file:com.qcadoo.mes.basic.shift.Shift.java

License:Open Source License

private DateRange buildDateRangeFrom(final TimeRange timeRange, final Date date) {
    DateTime dateTime = new DateTime(date);
    DateTime midnight = dateTime.withTimeAtStartOfDay();
    DateTime from;//from w  ww . ja  va2 s  . co m
    DateTime to;
    if (timeRange.startsDayBefore()) {
        if (dateTime.toLocalTime().isBefore(timeRange.getFrom())) {
            from = timeRange.getFrom().toDateTime(midnight.minusDays(1));
            to = timeRange.getTo().toDateTime(midnight);
        } else {
            from = timeRange.getFrom().toDateTime(midnight);
            to = timeRange.getTo().toDateTime(midnight.plusDays(1));
        }
    } else {
        from = timeRange.getFrom().toDateTime(midnight);
        to = timeRange.getTo().toDateTime(midnight);
    }
    return new DateRange(from.toDate(), to.toDate());
}

From source file:com.qcadoo.mes.basic.ShiftsServiceImpl.java

License:Open Source License

public Collection<ShiftHour> getHourForDay(final Entity shift, final Date dateFrom, final Date dateTo,
        final String day, final int offset) {
    if ((Boolean) shift.getField(day + WORKING_LITERAL)
            && StringUtils.hasText(shift.getStringField(day + HOURS_LITERAL))) {
        List<ShiftHour> hours = new ArrayList<ShiftHour>();

        LocalTime[][] dayHours = convertDayHoursToInt(shift.getStringField(day + HOURS_LITERAL));

        DateTime from = new DateTime(dateFrom).withSecondOfMinute(0);
        DateTime to = new DateTime(dateTo);

        DateTime current = from.plusDays(offset - from.getDayOfWeek());

        if (current.compareTo(from) < 0) {
            current = current.plusDays(7);
        }/*  w w  w. j av  a  2 s. c  o  m*/

        while (current.compareTo(to) <= 0) {
            for (LocalTime[] dayHour : dayHours) {
                hours.add(new ShiftHour(
                        current.withHourOfDay(dayHour[0].getHourOfDay())
                                .withMinuteOfHour(dayHour[0].getMinuteOfHour()).toDate(),
                        current.withHourOfDay(dayHour[1].getHourOfDay())
                                .withMinuteOfHour(dayHour[1].getMinuteOfHour()).toDate()));
            }
            current = current.plusDays(7);
        }

        return hours;
    } else {
        return Collections.emptyList();
    }
}

From source file:com.qcadoo.mes.productionPerShift.DateTimeRange.java

License:Open Source License

public DateTimeRange(final DateTime day, final TimeRange range) {
    DateTime from;// w  ww .j  ava  2  s . com
    DateTime to;
    if (range.startsDayBefore()) {
        to = range.getTo().toDateTime(day.plusDays(1));
    } else {
        to = range.getTo().toDateTime(day);
    }
    from = range.getFrom().toDateTime(day);
    interval = new Interval(from, to);
}

From source file:com.qcadoo.view.internal.components.ganttChart.GanttChartComponentState.java

License:Open Source License

@Override
protected void initializeContent(final JSONObject json) throws JSONException {

    JSONObject headerDataObject = json.getJSONObject("headerParameters");

    ZoomLevel zoomLevel = ZoomLevel.valueOf(headerDataObject.getString("scale"));

    String dateFromString = headerDataObject.getString("dateFrom");
    String dateToString = headerDataObject.getString("dateTo");

    DateTime now = new DateTime().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);

    Date dateFrom = now.plusDays(defaultStartDay).toDate();
    Date dateTo = now.plusDays(defaultEndDay).toDate();

    if (dateFromString == null || "".equals(dateFromString)) {
        dateFromErrorMessage = translate("errorMessage.emptyDate");
    } else {//from   w  ww  .j a v  a2s. c  o  m
        ValueAndError dateFromVaE = DATETYPE.toObject(null, dateFromString);
        if (dateFromVaE.getMessage() == null) {
            dateFrom = (Date) dateFromVaE.getValue();
        } else {
            dateFromErrorMessage = translate("errorMessage.dateNotValid");
        }
    }

    if (dateToString == null || "".equals(dateToString)) {
        dateToErrorMessage = translate("errorMessage.emptyDate");
    } else {
        ValueAndError dateToVaE = DATETYPE.toObject(null, dateToString);
        if (dateToVaE.getMessage() == null) {
            dateTo = (Date) dateToVaE.getValue();
        } else {
            dateToErrorMessage = translate("errorMessage.dateNotValid");
        }
    }

    scale = new GanttChartScaleImpl(this, zoomLevel, dateFrom, dateTo);

    if (dateFromErrorMessage == null && globalErrorMessage == null) {
        if (scale.isFromLargerThanTo()) {
            globalErrorMessage = translate("errorMessage.fromLargerThanTo");
        } else if (scale.isTooLargeRange()) {
            globalErrorMessage = translate("errorMessage.tooLargeRange",
                    String.valueOf(scale.getMaxRangeInMonths()));
        }
    }

    if (json.has("selectedEntityId")) {
        selectedEntityId = json.getLong("selectedEntityId");
    }

}