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.google.android.apps.paco.NonESMSignalGenerator.java

License:Open Source License

private DateTime scheduleWeekday(DateTime now) {
    DateTime nowMidnight = now.toDateMidnight().toDateTime();
    if (nowMidnight.getDayOfWeek() < DateTimeConstants.SATURDAY) { // jodatime starts with Monday = 0
        DateTime nextTimeToday = getNextTimeToday(now, nowMidnight);
        if (nextTimeToday != null) {
            return nextTimeToday;
        }/*w w  w.  j a  va  2 s  .co  m*/
    }
    DateTime nextDay = getNextScheduleDay(nowMidnight.plusDays(1));
    return getFirstScheduledTimeOnDay(nextDay);
}

From source file:com.google.android.apps.paco.NonESMSignalGenerator.java

License:Open Source License

private DateTime getNextScheduleDay(DateTime midnightTomorrow) {

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

    case SignalSchedule.WEEKDAY:
        int tomorrowDOW = midnightTomorrow.getDayOfWeek();
        if (tomorrowDOW > DateTimeConstants.FRIDAY) {
            return midnightTomorrow.plusDays(8 - tomorrowDOW);
        } else {//from   w  w  w  .ja  va 2  s . co m
            return midnightTomorrow;
        }

    case SignalSchedule.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 = SignalSchedule.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 SignalSchedule.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.google.android.apps.paco.NonESMSignalGenerator.java

License:Open Source License

private DateTime nextRepeatDaily(DateTime midnightTomorrow) {
    if (schedule.getRepeatRate() == 1) {
        return midnightTomorrow;
    }/*from w  w  w  . java  2s  .co  m*/
    int distanceBetweenStartAndTomorrow = Days
            .daysBetween(new DateTime(schedule.getBeginDate()).toDateMidnight(), midnightTomorrow).getDays();
    if (distanceBetweenStartAndTomorrow == 0 || distanceBetweenStartAndTomorrow == schedule.getRepeatRate()) {
        return midnightTomorrow;
    } else if (distanceBetweenStartAndTomorrow > schedule.getRepeatRate()) {
        int remainder = distanceBetweenStartAndTomorrow % schedule.getRepeatRate();
        return midnightTomorrow.plusDays(schedule.getRepeatRate() - remainder);
    } else {
        return midnightTomorrow.plusDays(schedule.getRepeatRate() - distanceBetweenStartAndTomorrow);
    }
}

From source file:com.google.android.apps.paco.TimeUtil.java

License:Open Source License

public static DateTime skipWeekends(DateTime plusDays) {
    if (plusDays.getDayOfWeek() == DateTimeConstants.SATURDAY) {
        return plusDays.plusDays(2);
    } else if (plusDays.getDayOfWeek() == DateTimeConstants.SUNDAY) {
        return plusDays.plusDays(1);
    }/*w ww .  j  av a  2 s.  c  om*/
    return plusDays;
}

From source file:com.google.sampling.experiential.server.DSQueryBuilder.java

License:Open Source License

private void compareDateRange(String key, String range, DateTimeZone jodaTimeZone) {
    DateTime startDate = null;
    DateTime endDate = null;/*ww w.j av  a  2 s.c o  m*/

    boolean keyCannedDateRange = isKeyCannedDateRange(key);
    boolean rangeCannedDateRange = isCannedDateRange(range);
    if (keyCannedDateRange || rangeCannedDateRange) {
        String rangeName = null;
        if (keyCannedDateRange) {
            rangeName = key.substring(1);
        } else {
            rangeName = range;
        }
        Interval interval;
        if (rangeName.equals("last_week")) {
            interval = getLastWeek(jodaTimeZone);
        } else if (rangeName.equals("last_month")) {
            interval = getLast4Weeks(jodaTimeZone);
        } else {
            throw new IllegalArgumentException("Unknown date range");
        }
        startDate = interval.getStart();
        endDate = interval.getEnd().plusDays(1);

    } else {
        Iterable<String> iterable = Splitter.on("-").split(range);
        Iterator<String> iter = iterable.iterator();
        if (!iter.hasNext()) {
            throw new IllegalArgumentException("Illformed Date Range: " + range);
        }
        String firstDate = iter.next();
        String secondDate = null;
        if (iter.hasNext()) {
            secondDate = iter.next();
        }

        startDate = newDateTimeFromString(firstDate, jodaTimeZone);
        endDate = null;
        if (secondDate != null && !secondDate.isEmpty()) {
            endDate = newDateTimeFromString(secondDate, jodaTimeZone).plusDays(1);
        } else {
            endDate = startDate.plusDays(1);
        }
    }
    jdoQuery.addFilters("when >= startDateParam", "when <= endDateParam");
    jdoQuery.declareParameters("java.util.Date startDateParam", "java.util.Date endDateParam");
    jdoQuery.addParameterObjects(startDate.toDate(), endDate.toDate());
}

From source file:com.google.sampling.experiential.server.JDOQueryBuilder.java

License:Open Source License

private void compareDateRange(String key, String range, DateTimeZone jodaTimeZone) {
    DateTime startDate = null;
    DateTime endDate = null;// w  ww  .j  a v a  2 s .c  om

    boolean keyCannedDateRange = isKeyCannedDateRange(key);
    boolean rangeCannedDateRange = isCannedDateRange(range);
    if (keyCannedDateRange || rangeCannedDateRange) {
        String rangeName = null;
        if (keyCannedDateRange) {
            rangeName = key.substring(1);
        } else {
            rangeName = range;
        }
        Interval interval;
        if (rangeName.equals("last_week")) {
            interval = getLastWeek(jodaTimeZone);
        } else if (rangeName.equals("last_month")) {
            interval = getLast4Weeks(jodaTimeZone);
        } else {
            throw new IllegalArgumentException("Unknown date range");
        }
        startDate = interval.getStart();
        endDate = interval.getEnd().plusDays(1);

    } else {
        Iterable<String> iterable = Splitter.on("-").split(range);
        Iterator<String> iter = iterable.iterator();
        if (!iter.hasNext()) {
            throw new IllegalArgumentException("Illformed Date Range: " + range);
        }
        String firstDate = iter.next();
        String secondDate = null;
        if (iter.hasNext()) {
            secondDate = iter.next();
        }

        startDate = newDateTimeFromDateString(firstDate, jodaTimeZone);
        endDate = null;
        if (secondDate != null && !secondDate.isEmpty()) {
            endDate = newDateTimeFromDateString(secondDate, jodaTimeZone).plusDays(1);
        } else {
            endDate = startDate.plusDays(1);
        }
    }
    jdoQuery.addFilters("when >= startDateParam", "when <= endDateParam");
    jdoQuery.declareParameters("java.util.Date startDateParam", "java.util.Date endDateParam");
    jdoQuery.addParameterObjects(startDate.toDate(), endDate.toDate());
}

From source file:com.google.sampling.experiential.server.JDOQueryBuilder.java

License:Open Source License

private void compareDateTimeRange(String key, String range, DateTimeZone jodaTimeZone) {
    DateTime startDate = null;
    DateTime endDate = null;/* w ww. ja  v  a 2 s.co  m*/

    Iterable<String> iterable = Splitter.on("--").split(range);
    Iterator<String> iter = iterable.iterator();
    if (!iter.hasNext()) {
        throw new IllegalArgumentException("Illformed Date Range: " + range);
    }
    String firstDate = iter.next();
    String secondDate = null;
    if (iter.hasNext()) {
        secondDate = iter.next();
    }

    startDate = newDateTimeFromDateTimeString(firstDate, jodaTimeZone);
    endDate = null;
    if (secondDate != null && !secondDate.isEmpty()) {
        endDate = newDateTimeFromDateTimeString(secondDate, jodaTimeZone).plusDays(1);
    } else {
        endDate = startDate.plusDays(1);
    }

    jdoQuery.addFilters("when >= startDateParam", "when <= endDateParam");
    jdoQuery.declareParameters("java.util.Date startDateParam", "java.util.Date endDateParam");
    jdoQuery.addParameterObjects(startDate.toDate(), endDate.toDate());
}

From source file:com.guavabot.alarmpreference.Alarm.java

License:Apache License

/**
 * Calculates the next time that the alarm should go off.
 * // w  w  w  .  java2s .  c o  m
 * @return Next trigger time as a UNIX timestamp or {@link Long#MAX_VALUE} if alarm is off.
 */
public long getNextTrigger() {
    if (mAlarmOn) {
        DateTime alarmDt = mTime.toDateTimeToday();
        for (int i = 0; i < 8; i++) {
            if ((mWeeklyAlarms & (1 << (alarmDt.getDayOfWeek() - 1))) != 0) {
                //if today, we make sure the time is not in the past
                if (i > 0 || alarmDt.isAfterNow()) {
                    return alarmDt.getMillis();
                }
            }
            alarmDt = alarmDt.plusDays(1);
        }
    }
    return Long.MAX_VALUE;
}

From source file:com.hack23.cia.web.impl.ui.application.views.common.chartfactory.impl.AbstractGhantChartManagerImpl.java

License:Apache License

/**
 * Strip dates after current date./*from w w w .j a  v  a  2 s  .com*/
 *
 * @param toDate
 *            the to date
 * @return the date
 */
private static final Date stripDatesAfterCurrentDate(final Date toDate) {
    final DateTime currentTime = new DateTime();

    if (currentTime.isBefore(toDate.getTime())) {
        return currentTime.plusDays(1).toDate();
    } else {
        return toDate;
    }
}

From source file:com.indeed.imhotep.builder.tsv.TsvConverter.java

License:Apache License

public static ShardInfo.DateTimeRange parseDateTime(String shardId) {
    try {/*  w  w  w .  j ava  2s .c  o m*/
        if (shardId.length() > 16) {
            final DateTime start = yyyymmddhh.parseDateTime(shardId.substring(5, 16));
            final DateTime end = yyyymmddhh.parseDateTime(shardId.substring(17, 28));
            return new ShardInfo.DateTimeRange(start, end);
        } else if (shardId.length() > 13) {
            final DateTime start = yyyymmddhh.parseDateTime(shardId.substring(5, 16));
            final DateTime end = start.plusHours(1);
            return new ShardInfo.DateTimeRange(start, end);
        } else {
            final DateTime start = yyyymmdd.parseDateTime(shardId.substring(5, 13));
            final DateTime end = start.plusDays(1);
            return new ShardInfo.DateTimeRange(start, end);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to get time range from file name: " + shardId, e);
    }
}