Example usage for org.joda.time LocalDateTime plus

List of usage examples for org.joda.time LocalDateTime plus

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime plus.

Prototype

public LocalDateTime plus(ReadablePeriod period) 

Source Link

Document

Returns a copy of this datetime with the specified period added.

Usage

From source file:cherry.goods.util.LocalDateTimeUtil.java

License:Apache License

/**
 * (TO)???//from   w ww. j ava 2 s  .  c o  m
 * 
 * @param to ??
 * @return ????(TO)??
 */
public static LocalDateTime rangeTo(LocalDateTime to) {
    if (to == null) {
        return null;
    }
    return to.plus(unitOfTime);
}

From source file:com.axelor.apps.crm.service.EventService.java

License:Open Source License

@Transactional
public void addRecurrentEventsByWeeks(Event event, int periodicity, int endType, int repetitionsNumber,
        LocalDate endDate, Map<Integer, Boolean> daysCheckedMap) {
    Event lastEvent = event;//from   w w w .ja  va  2s. c om
    List<Integer> list = new ArrayList<Integer>();
    for (int day : daysCheckedMap.keySet()) {
        list.add(day);
    }
    Collections.sort(list);
    if (endType == 1) {
        int repeated = 0;
        Event copy = eventRepo.copy(lastEvent, false);
        copy.setParentEvent(lastEvent);
        int dayOfWeek = copy.getStartDateTime().getDayOfWeek();
        LocalDateTime nextDateTime = new LocalDateTime();
        if (dayOfWeek < list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
        } else if (dayOfWeek > list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
        }
        Duration dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

        for (Integer integer : list) {
            nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
            copy.setStartDateTime(nextDateTime);
            copy.setEndDateTime(nextDateTime.plus(dur));
            eventRepo.save(copy);
            lastEvent = copy;
            repeated++;
        }

        while (repeated < repetitionsNumber) {
            copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(lastEvent);
            copy.setStartDateTime(copy.getStartDateTime().plusWeeks(periodicity));
            copy.setEndDateTime(copy.getEndDateTime().plusWeeks(periodicity));

            dayOfWeek = copy.getStartDateTime().getDayOfWeek();
            nextDateTime = new LocalDateTime();
            if (dayOfWeek < list.get(0)) {
                nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
            } else if (dayOfWeek > list.get(0)) {
                nextDateTime = new LocalDateTime(
                        copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
            }
            dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

            for (Integer integer : list) {
                nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;
                repeated++;
            }
        }
    } else {

        Event copy = eventRepo.copy(lastEvent, false);
        copy.setParentEvent(lastEvent);
        int dayOfWeek = copy.getStartDateTime().getDayOfWeek();
        LocalDateTime nextDateTime = new LocalDateTime();
        if (dayOfWeek < list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
        } else if (dayOfWeek > list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
        }
        Duration dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

        for (Integer integer : list) {
            nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
            copy.setStartDateTime(nextDateTime);
            copy.setEndDateTime(nextDateTime.plus(dur));
            eventRepo.save(copy);
            lastEvent = copy;
        }

        while (!copy.getStartDateTime().plusWeeks(periodicity).isAfter(endDate)) {
            copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(lastEvent);
            copy.setStartDateTime(copy.getStartDateTime().plusWeeks(periodicity));
            copy.setEndDateTime(copy.getEndDateTime().plusWeeks(periodicity));

            dayOfWeek = copy.getStartDateTime().getDayOfWeek();
            nextDateTime = new LocalDateTime();
            if (dayOfWeek < list.get(0)) {
                nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
            } else if (dayOfWeek > list.get(0)) {
                nextDateTime = new LocalDateTime(
                        copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
            }
            dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

            for (Integer integer : list) {
                nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;
            }
        }
    }
}

From source file:com.axelor.apps.crm.service.EventService.java

License:Open Source License

@Transactional
public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber,
        LocalDate endDate, int monthRepeatType) {
    Event lastEvent = event;//  w ww  . j  av a 2  s .  c o  m
    if (monthRepeatType == 1) {
        int dayOfMonth = event.getStartDateTime().getDayOfMonth();
        if (endType == 1) {
            int repeated = 0;
            while (repeated != repetitionsNumber) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth()
                        .getMaximumValue() >= dayOfMonth) {
                    copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity));
                    copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity));
                    eventRepo.save(copy);
                    repeated++;
                    lastEvent = copy;
                }
            }
        } else {
            while (!lastEvent.getStartDateTime().plusMonths(periodicity).isAfter(endDate)) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth()
                        .getMaximumValue() >= dayOfMonth) {
                    copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity));
                    copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity));
                    eventRepo.save(copy);
                    lastEvent = copy;
                }
            }
        }
    }

    else {
        int dayOfWeek = event.getStartDateTime().getDayOfWeek();
        int positionInMonth = 0;
        if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
            positionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
        } else {
            positionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
        }

        if (endType == 1) {
            int repeated = 0;
            while (repeated != repetitionsNumber) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                LocalDateTime nextDateTime = new LocalDateTime(copy.getStartDateTime());
                nextDateTime.plusMonths(periodicity);
                int nextDayOfWeek = nextDateTime.getDayOfWeek();
                if (nextDayOfWeek > dayOfWeek) {
                    nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
                } else {
                    nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
                }
                int nextPositionInMonth = 0;
                if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                    nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
                } else {
                    nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
                }
                if (nextPositionInMonth > positionInMonth) {
                    nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
                } else {
                    nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
                }
                Duration dur = new Duration(copy.getStartDateTime().toDateTime(),
                        copy.getEndDateTime().toDateTime());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                repeated++;
                lastEvent = copy;
            }
        } else {
            LocalDateTime nextDateTime = new LocalDateTime(lastEvent.getStartDateTime());
            nextDateTime.plusMonths(periodicity);
            int nextDayOfWeek = nextDateTime.getDayOfWeek();
            if (nextDayOfWeek > dayOfWeek) {
                nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
            } else {
                nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
            }
            int nextPositionInMonth = 0;
            if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
            } else {
                nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
            }
            if (nextPositionInMonth > positionInMonth) {
                nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
            } else {
                nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
            }
            while (!nextDateTime.isAfter(endDate)) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);

                Duration dur = new Duration(copy.getStartDateTime().toDateTime(),
                        copy.getEndDateTime().toDateTime());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;

                nextDateTime = new LocalDateTime(lastEvent.getStartDateTime());
                nextDateTime.plusMonths(periodicity);
                nextDayOfWeek = nextDateTime.getDayOfWeek();
                if (nextDayOfWeek > dayOfWeek) {
                    nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
                } else {
                    nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
                }
                nextPositionInMonth = 0;
                if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                    nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
                } else {
                    nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
                }
                if (nextPositionInMonth > positionInMonth) {
                    nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
                } else {
                    nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
                }
            }
        }
    }
}

From source file:com.effektif.workflow.api.model.AfterRelativeTime.java

License:Apache License

public LocalDateTime resolve(LocalDateTime base) {
    if (this.duration == null || this.durationUnit == null) {
        return null;
    }//from   w  w w .j a v  a  2 s.c  om

    ReadablePeriod period = null;
    if (DAYS.equals(durationUnit)) {
        period = Days.days(getDurationAsInt());
    } else if (WEEKS.equals(durationUnit)) {
        period = Weeks.weeks(getDurationAsInt());
    } else if (HOURS.equals(durationUnit)) {
        period = Hours.hours(getDurationAsInt());
    } else if (MONTHS.equals(durationUnit)) {
        period = Months.months(getDurationAsInt());
    } else if (YEARS.equals(durationUnit)) {
        period = Years.years(getDurationAsInt());
    } else if (MINUTES.equals(durationUnit)) {
        period = Minutes.minutes(getDurationAsInt());
    } else {
        return null;
    }

    LocalDateTime time = base.plus(period);

    if (atHour != null) {
        LocalDateTime atTime = time.withTime(atHour, atMinute != null ? atMinute : 0, 0, 0);
        if (atTime.isBefore(time)) {
            time = atTime.plusDays(1);
        } else {
            time = atTime;
        }
    } else if (isDayResolutionOrBigger()) {
        time = time.withTime(23, 59, 59, 999);
    }

    return time;
}

From source file:control.TimeInvestmentControl.java

/**
 * Formlet med denne metode er at tlle n op p et rum i en given liste af
 * rum for hver gang dette rum er blevet tildelt en medarbejder ved hjlp af
 * et TimeInvestment-objekt. P en given vagts starttidspunkt. P denne mde
 * kan man stole p at alle rum i den givne liste har et count felt der
 * passer til den rigtige dato, frem for at vre tidslst (feltet angiver
 * kun et count, og er ligeglad med tiden, denne funktionalitet sikrer at
 * tiden stemmer).//from   ww w.j av a 2  s.  c  o  m
 *
 * @param rooms
 * @param currentShift
 * @param timeInvestments
 */
public void countDateAssignmentsOfRoom(ArrayList<Room> rooms, TimeInvestment currentShift,
        ArrayList<TimeInvestment> timeInvestments) {

    //Nulstil roomObjekternes count felt.
    for (Room room : rooms) {
        room.setCount(0);
    }

    if (timeInvestments.size() > 0) {
        for (int i = 0; i < timeInvestments.size(); i++) {
            String roomName = timeInvestments.get(i).getRoom().getRoomName();
            //Liner sgning. Hvis det givne rum findes s skal dets tller,
            //tlle n op.
            for (int j = 0; j < rooms.size(); j++) {
                if (roomName.equals(rooms.get(j).getRoomName())) {
                    TimeInvestment currentTI = timeInvestments.get(i);

                    LocalDateTime date = currentShift.getStartTime();
                    LocalDateTime periodStart = currentTI.getStartTime();
                    LocalDateTime periodEnd = new LocalDateTime(periodStart);
                    periodEnd = periodEnd.plus(currentTI.getHours());
                    periodEnd = periodEnd.plus(currentTI.getMinutes());
                    if (Xray.getInstance().isDateInPeriod(date, periodStart, periodEnd)) {
                        rooms.get(j).increment();
                        break; //break OK i sgning for optimering.
                    }
                    break;
                }
            }
        }

    }
}

From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java

License:Open Source License

public void addToField(Granulaarsus field, Period period, int direction) {
    if (field.compareByCoarseRank(Granulaarsus.WEEK_OF_YEAR) == -1) {
        // ---- paev, tund, minut
        LocalDateTime ajaFookus = getAsLocalDateTime();
        try {// ww w .  j a  v  a2 s.  co m
            if (direction > 0) {
                ajaFookus = ajaFookus.plus(period);
            } else {
                ajaFookus = ajaFookus.minus(period);
            }
            this.functionOtherThanSetUsed = true;
        } catch (Exception e) {
            // Erindi korral jaabki muutmata
        }
        this.underlyingTime = ajaFookus.toLocalTime();
        this.underlyingDate = ajaFookus.toLocalDate();
        if (field.compareByCoarseRank(Granulaarsus.DAY_OF_MONTH) == 0) {
            this.dateModified = true;
            updateTimeRepresentation(Granulaarsus.HOUR_OF_DAY, null, false, ADD_TYPE_OPERATION);
        } else {
            updateTimeRepresentation(Granulaarsus.MINUTE, null, false, ADD_TYPE_OPERATION);
        }
        if (dateModified) {
            updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION);
        }
    } else {
        // ---- nadal, kuu, aasta, sajand
        try {
            if (direction > 0) {
                this.underlyingDate = (this.underlyingDate).plus(period);
            } else {
                this.underlyingDate = (this.underlyingDate).minus(period);
            }
            this.functionOtherThanSetUsed = true;
            this.dateModified = true;
        } catch (Exception e) {
            // Erindi korral jaabki muutmata
        }
        if (field == Granulaarsus.MONTH || field == Granulaarsus.WEEK_OF_YEAR) {
            updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION);
        }
        if (field == Granulaarsus.YEAR) {
            updateDateRepresentation(Granulaarsus.MONTH, null, false, ADD_TYPE_OPERATION);
        }
        if (field == Granulaarsus.CENTURY_OF_ERA) {
            updateDateRepresentation(Granulaarsus.YEAR, null, false, ADD_TYPE_OPERATION);
        }
    }
}

From source file:org.cowboyprogrammer.org.OrgTimestamp.java

License:Open Source License

/**
 * Return the next repetition of this time, even if
 * it is already in the future. Null if no repeat.
 *//*  www .j a  va  2 s .co  m*/
public LocalDateTime getNextRepetition() {
    if (repeater == null)
        return null;

    final LocalDateTime now = LocalDateTime.now();
    LocalDateTime next = date.withDayOfMonth(date.getDayOfMonth());

    if (repeater.startsWith("++")) {
        if (now.isAfter(next)) {
            // Just get it into the future
            while (now.isAfter(next)) {
                next = next.plus(repeatPeriod);
            }
        } else {
            // Already in future, just jump
            next = next.plus(repeatPeriod);
        }
    } else if (repeater.startsWith(".+")) {
        // Count from NOW
        next = now.plus(repeatPeriod);
    } else { // + or
        next = next.plus(repeatPeriod);
    }

    return next;
}

From source file:org.cowboyprogrammer.org.OrgTimestamp.java

License:Open Source License

/**
 * Returns null if no repeater is set. Otherwise the next repetition of this
 * time which is in the future. If it is already in the future, it will
 * return that.//from   w  ww .j ava  2 s .co  m
 */
public LocalDateTime getNextFutureRepetition() {
    if (repeater == null) {
        return null;
    }
    final LocalDateTime now = LocalDateTime.now();
    if (now.isBefore(date)) {
        // Already in future
        return date;
    }
    // In this case, + and ++ have the same behaviour
    if (repeater.startsWith("+")) {
        LocalDateTime next = date.plus(repeatPeriod);
        // Just get it into the future
        while (now.isAfter(next)) {
            next = next.plus(repeatPeriod);
        }
        return next;
    } else {
        // Count from NOW
        return now.plus(repeatPeriod);
    }
}

From source file:org.gnucash.android.model.Recurrence.java

License:Apache License

/**
 * Computes the number of occurrences of this recurrences between start and end date
 * <p>If there is no end date, it returns -1</p>
 * @return Number of occurrences, or -1 if there is no end date
 *//*from   w  w  w  .j  a  va2s  .  com*/
public int getCount() {
    if (mPeriodEnd == null)
        return -1;

    int multiple = mPeriodType.getMultiplier();
    ReadablePeriod jodaPeriod;
    switch (mPeriodType) {
    case DAY:
        jodaPeriod = Days.days(multiple);
        break;
    case WEEK:
        jodaPeriod = Weeks.weeks(multiple);
        break;
    case MONTH:
        jodaPeriod = Months.months(multiple);
        break;
    case YEAR:
        jodaPeriod = Years.years(multiple);
        break;
    default:
        jodaPeriod = Months.months(multiple);
    }
    int count = 0;
    LocalDateTime startTime = new LocalDateTime(mPeriodStart.getTime());
    while (startTime.toDateTime().getMillis() < mPeriodEnd.getTime()) {
        ++count;
        startTime = startTime.plus(jodaPeriod);
    }
    return count;

    /*
            //this solution does not use looping, but is not very accurate
            
            int multiplier = mPeriodType.getMultiplier();
            LocalDateTime startDate = new LocalDateTime(mPeriodStart.getTime());
            LocalDateTime endDate = new LocalDateTime(mPeriodEnd.getTime());
            switch (mPeriodType){
    case DAY:
        return Days.daysBetween(startDate, endDate).dividedBy(multiplier).getDays();
    case WEEK:
        return Weeks.weeksBetween(startDate, endDate).dividedBy(multiplier).getWeeks();
    case MONTH:
        return Months.monthsBetween(startDate, endDate).dividedBy(multiplier).getMonths();
    case YEAR:
        return Years.yearsBetween(startDate, endDate).dividedBy(multiplier).getYears();
    default:
        return -1;
            }
    */
}

From source file:org.openmrs.module.referencedemodata.ReferenceDemoDataActivator.java

License:Open Source License

private Visit createDemoVisit(Patient patient, List<VisitType> visitTypes, Location location,
        boolean shortVisit) {
    LocalDateTime visitStart = LocalDateTime.now().minus(Period.days(randomBetween(0, 365 * 2)).withHours(3)); // past 2 years
    if (!shortVisit) {
        visitStart = visitStart.minus(Period.days(ADMISSION_DAYS_MAX + 1)); // just in case the start is today, back it up a few days.
    }/*from  w  w w .j a  v a 2 s. c  o  m*/
    Visit visit = new Visit(patient, randomArrayEntry(visitTypes), visitStart.toDate());
    visit.setLocation(location);
    LocalDateTime vitalsTime = visitStart.plus(Period.minutes(randomBetween(1, 60)));
    visit.addEncounter(createDemoVitalsEncounter(patient, vitalsTime.toDate()));
    LocalDateTime visitNoteTime = visitStart.plus(Period.minutes(randomBetween(60, 120)));
    visit.addEncounter(createVisitNote(patient, visitNoteTime.toDate(), location));
    if (shortVisit) {
        LocalDateTime visitEndTime = visitNoteTime.plus(Period.minutes(30));
        visit.setStopDatetime(visitEndTime.toDate());
    } else {
        // admit now and discharge a few days later
        Location admitLocation = Context.getLocationService().getLocation("Inpatient Ward");
        visit.addEncounter(createEncounter("Admission", patient, visitNoteTime.toDate(), admitLocation));
        LocalDateTime dischargeDateTime = visitNoteTime
                .plus(Period.days(randomBetween(ADMISSION_DAYS_MIN, ADMISSION_DAYS_MAX)));
        visit.addEncounter(createEncounter("Discharge", patient, dischargeDateTime.toDate(), admitLocation));
        visit.setStopDatetime(dischargeDateTime.toDate());
    }
    return visit;
}