Example usage for org.joda.time LocalDateTime toDateTime

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

Introduction

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

Prototype

public DateTime toDateTime() 

Source Link

Document

Converts this object to a DateTime using the default zone.

Usage

From source file:de.avanux.smartapplianceenabler.appliance.ConsecutiveDaysTimeframe.java

License:Open Source License

public List<TimeframeInterval> getIntervals(LocalDateTime now) {
    if (start != null && end != null) {
        LocalDateTime earliestStartNextOccurrence = start.toNextOccurrence(now);
        LocalDateTime latestEndNextOccurrence = end.toNextOccurrence(now);
        LocalDateTime earliestStartDateTime = earliestStartNextOccurrence;
        if (latestEndNextOccurrence.isBefore(earliestStartNextOccurrence)
                && now.isBefore(latestEndNextOccurrence)) {
            earliestStartDateTime = start.toLastOccurrence(now);
        }/*from w  ww  .  j a v a2  s  . c  om*/
        LocalDateTime latestEndDateTime = end.toNextOccurrence(earliestStartDateTime);
        Interval interval = new Interval(earliestStartDateTime.toDateTime(), latestEndDateTime.toDateTime())
                .withChronology(ISOChronology.getInstance());
        TimeframeInterval timeframeInterval = new TimeframeInterval(this, interval);
        return Collections.singletonList(timeframeInterval);
    }
    return null;
}

From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframe.java

License:Open Source License

protected Interval buildMidnightAdjustedInterval(LocalDateTime now) {
    if (start != null && end != null) {
        LocalDateTime earliestStartDateTime = new LocalDate(now).toLocalDateTime(start.toLocalTime());
        LocalDateTime latestEndDateTime = new LocalDate(now).toLocalDateTime(end.toLocalTime());
        if (isOverMidnight(earliestStartDateTime, latestEndDateTime)) {
            if (now.toLocalTime().isAfter(start.toLocalTime())) {
                // before midnight
                latestEndDateTime = latestEndDateTime.plusHours(24);
            } else if (now.toLocalTime().isBefore(end.toLocalTime())) {
                // after midnight, before end
                earliestStartDateTime = earliestStartDateTime.minusHours(24);
            } else {
                // after midnight, after end
                latestEndDateTime = latestEndDateTime.plusHours(24);
            }/*  w  ww.j a va2 s  . c  om*/
        }
        return new Interval(earliestStartDateTime.toDateTime(), latestEndDateTime.toDateTime())
                .withChronology(ISOChronology.getInstance());
    }
    return null;
}

From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframe.java

License:Open Source License

@Override
public List<TimeframeInterval> getIntervals(LocalDateTime now) {
    List<TimeframeInterval> intervals = new ArrayList<>();
    if (start != null && end != null) {
        Interval interval = buildMidnightAdjustedInterval(now);
        List<Integer> dowValues = getDaysOfWeekValues();
        // if today's interval already ended we ignore today
        int dayOffset = (interval.getEnd().isBefore(now.toDateTime()) ? 1 : 0);
        for (int i = dayOffset; i < 7 + dayOffset; i++) {
            DateTime timeFrameStart = interval.getStart().plusDays(i);
            DateTime timeFrameEnd = interval.getEnd().plusDays(i);
            if (dowValues != null) {
                int dow = timeFrameStart.getDayOfWeek();
                if (dowValues.contains(DOW_HOLIDAYS) && isHoliday(timeFrameStart.toLocalDate())) {
                    dow = DOW_HOLIDAYS;/*  ww w . j  av a  2  s. c o m*/
                }
                if (dowValues.contains(dow)) {
                    intervals.add(new TimeframeInterval(this, new Interval(timeFrameStart, timeFrameEnd)));
                }
            } else {
                intervals.add(new TimeframeInterval(this, new Interval(timeFrameStart, timeFrameEnd)));
            }
        }
    }
    return intervals;
}

From source file:de.avanux.smartapplianceenabler.appliance.RunningTimeMonitor.java

License:Open Source License

/**
 * Updates remainingMinRunningTime for the given instant. The value may become negative!
 * Subsequent calls to this method within one second are omitted.
 * @param now//from ww w .  jav a 2  s.  co m
 */
protected void update(LocalDateTime now) {
    // update not more than once per second in order to avoid spamming the log
    if (lastUpdate == null || now.isBefore(lastUpdate)
            || new Interval(lastUpdate.toDateTime(), now.toDateTime()).toDurationMillis() > 1000) {
        activateTimeframeInterval(now, schedules);
        deactivateExpiredTimeframeInterval(now);
        logger.debug("activeTimeframeInterval=" + activeTimeframeInterval + " statusChangedAt="
                + statusChangedAt + " intervalBegin=" + intervalBegin + " running=" + running);

        Interval interval = null;
        if (running) {
            // running
            if (intervalBegin == null) {
                // running was set to true after interval begin
                interval = new Interval(statusChangedAt.toDateTime(), now.toDateTime());
            } else {
                // no status change in interval
                interval = new Interval(intervalBegin.toDateTime(), now.toDateTime());
            }
            intervalBegin = now;
        } else if (intervalBegin != null && statusChangedAt != null) {
            // running was set to false after interval begin
            interval = new Interval(intervalBegin.toDateTime(), statusChangedAt.toDateTime());
            intervalBegin = null;
            statusChangedAt = null;
        }
        if (interval != null && remainingMinRunningTime != null && remainingMaxRunningTime != null) {
            int intervalSeconds = Double.valueOf(interval.toDuration().getMillis() / 1000).intValue();
            remainingMinRunningTime = remainingMinRunningTime - intervalSeconds;
            remainingMaxRunningTime = remainingMaxRunningTime - intervalSeconds;
        }
        lastUpdate = now;
    }
}

From source file:de.avanux.smartapplianceenabler.appliance.RunningTimeMonitor.java

License:Open Source License

/**
 * Deactivate the active timeframe interval if it is expired.
 * @param now/* www .  j av a  2s. co  m*/
 */
protected void deactivateExpiredTimeframeInterval(LocalDateTime now) {
    if (activeTimeframeInterval != null) {
        if (now.toDateTime().isAfter(activeTimeframeInterval.getInterval().getEnd())) {
            activateTimeframeInterval(now, (TimeframeInterval) null);
        }
    }
}

From source file:de.avanux.smartapplianceenabler.appliance.Schedule.java

License:Open Source License

/**
 * Returns the current or next timeframe if the remaining time is greater than maximum running time; otherwise the next timeframe is returned.
 * @param now the time reference//from  w  w  w.jav a2 s.  c om
 * @param schedules the list of timeframes to choose from (current timeframe has to be first)
 * @param onlyAlreadyStarted consider only timeframe intervals already started
 * @param onlySufficient if true consider timeframe already started only if time to interval end exceeds min running time
 * @return the next timeframe becoming valid or null
 */
public static TimeframeInterval getCurrentOrNextTimeframeInterval(LocalDateTime now, List<Schedule> schedules,
        boolean onlyAlreadyStarted, boolean onlySufficient) {
    if (schedules == null || schedules.size() == 0) {
        return null;
    }
    Map<Long, TimeframeInterval> startDelayOfTimeframeInterval = new TreeMap<>();
    for (Schedule schedule : schedules) {
        Timeframe timeframe = schedule.getTimeframe();
        timeframe.setSchedule(schedule);
        List<TimeframeInterval> timeframeIntervals = timeframe.getIntervals(now);
        for (TimeframeInterval timeframeInterval : timeframeIntervals) {
            Interval interval = timeframeInterval.getInterval();
            if (interval.contains(now.toDateTime())) {
                // interval already started ...
                if (onlySufficient) {
                    if (now.plusSeconds(schedule.getMaxRunningTime()).plusSeconds(additionalRunningTime)
                            .isBefore(new LocalDateTime(interval.getEnd()))) {
                        // ... with remaining running time sufficient
                        return timeframeInterval;
                    }
                } else {
                    return timeframeInterval;
                }
            } else if (!onlyAlreadyStarted) {
                // interval starts in future
                startDelayOfTimeframeInterval.put(interval.getStartMillis() - now.toDateTime().getMillis(),
                        timeframeInterval);
            }
        }
    }
    if (startDelayOfTimeframeInterval.size() > 0) {
        Long startDelay = startDelayOfTimeframeInterval.keySet().iterator().next();
        return startDelayOfTimeframeInterval.get(startDelay);
    }
    return null;
}

From source file:de.avanux.smartapplianceenabler.semp.webservice.SempController.java

License:Open Source License

private PlanningRequest createPlanningRequest(ApplianceLogger applianceLogger, LocalDateTime now,
        Appliance appliance) {// www . j a  va 2  s  .c  om
    PlanningRequest planningRequest = null;
    RunningTimeMonitor runningTimeMonitor = appliance.getRunningTimeMonitor();
    if (runningTimeMonitor != null) {
        List<de.avanux.smartapplianceenabler.semp.webservice.Timeframe> sempTimeFrames = new ArrayList<de.avanux.smartapplianceenabler.semp.webservice.Timeframe>();
        List<Schedule> schedules = runningTimeMonitor.getSchedules();
        TimeframeInterval timeframeInterval = runningTimeMonitor.getActiveTimeframeInterval();
        if (schedules != null && schedules.size() > 0) {
            applianceLogger.debug("Active schedules: " + schedules.size());
            TimeframeInterval activeTimeframeInterval = runningTimeMonitor.getActiveTimeframeInterval();
            addSempTimeFrame(applianceLogger, runningTimeMonitor, timeframeInterval, appliance, sempTimeFrames,
                    now);

            Interval considerationInterval = new Interval(now.toDateTime(), now.plusDays(2).toDateTime());
            List<TimeframeInterval> timeFrameIntervals = Schedule.findTimeframeIntervals(now,
                    considerationInterval, runningTimeMonitor.getSchedules());
            for (TimeframeInterval timeframeIntervalOfSchedule : timeFrameIntervals) {
                Schedule schedule = timeframeIntervalOfSchedule.getTimeframe().getSchedule();
                addSempTimeFrame(applianceLogger, appliance, sempTimeFrames, schedule,
                        timeframeIntervalOfSchedule.getInterval(), schedule.getMinRunningTime(),
                        schedule.getMaxRunningTime(), now);
            }
        } else if (timeframeInterval != null) {
            applianceLogger.debug("Active timeframe interval found");
            addSempTimeFrame(applianceLogger, runningTimeMonitor, timeframeInterval, appliance, sempTimeFrames,
                    now);
        } else {
            applianceLogger.debug("No timeframes found");
        }

        if (sempTimeFrames.size() > 0) {
            planningRequest = new PlanningRequest();
            planningRequest.setTimeframes(sempTimeFrames);
        } else {
            applianceLogger.debug("No planning requests created");
            return null;
        }
    }
    return planningRequest;
}

From source file:de.avanux.smartapplianceenabler.semp.webservice.SempController.java

License:Open Source License

protected de.avanux.smartapplianceenabler.semp.webservice.Timeframe createSempTimeFrame(
        ApplianceLogger applianceLogger, String deviceId, Schedule schedule, Interval interval,
        long minRunningTime, long maxRunningTime, LocalDateTime now) {
    Long earliestStart = 0l;/* w w w  . ja va2s . c  o  m*/
    DateTime start = interval.getStart();
    DateTime end = interval.getEnd();
    if (start.isAfter(now.toDateTime())) {
        earliestStart = Double.valueOf(new Interval(now.toDateTime(), start).toDurationMillis() / 1000)
                .longValue();
    }
    LocalDateTime nowBeforeEnd = new LocalDateTime(now);
    if (now.toDateTime().isAfter(end)) {
        nowBeforeEnd = now.minusHours(24);
    }
    Long latestEnd = Double.valueOf(new Interval(nowBeforeEnd.toDateTime(), end).toDurationMillis() / 1000)
            .longValue();
    return createSempTimeFrame(applianceLogger, deviceId, earliestStart, latestEnd, minRunningTime,
            maxRunningTime);
}

From source file:de.hh.changeRing.infrastructure.eclipselink.JodaLocalDateTimeConverter.java

License:Open Source License

@Override
public java.util.Date toDatabaseLayerType(org.joda.time.LocalDateTime objectValue) {
    return objectValue.toDateTime().toDate();
}

From source file:divconq.scheduler.Scheduler.java

License:Open Source License

public ISchedule runAt(Task work, LocalDate date, Period period) {
    LocalDateTime ldt = date.toLocalDateTime(new LocalTime(0, 0).plus(period));
    return this.runAt(work, ldt.toDateTime());
}