List of usage examples for org.joda.time DateTime getDayOfWeek
public int getDayOfWeek()
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; }//from w w w .jav a2 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 ww w .j a v a 2s.com*/ 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
DateTime getNthDOWOfMonth(DateTime midnightTomorrow, Integer nthOfMonth, Integer dow) { int dtconstDow = dow == 0 ? 7 : dow; DateTime first = midnightTomorrow.withDayOfMonth(1); if (first.getDayOfWeek() > dtconstDow) { return first.plusWeeks(nthOfMonth).withDayOfWeek(dtconstDow); } else {// w w w .j a v a 2 s.c om return first.plusWeeks(nthOfMonth - 1).withDayOfWeek(dtconstDow); } }
From source file:com.google.android.apps.paco.NonESMSignalGenerator.java
License:Open Source License
private DateTime nextRepeatWeekly(DateTime midnightNextDay) { if (schedule.getRepeatRate() == 1) { return midnightNextDay; }/*from w ww .j av a 2 s . c om*/ int distanceBetweenStartAndTomorrow = Weeks .weeksBetween(new DateTime(schedule.getBeginDate()).toDateMidnight(), midnightNextDay).getWeeks(); if (distanceBetweenStartAndTomorrow == 0 || distanceBetweenStartAndTomorrow == schedule.getRepeatRate()) { if ((distanceBetweenStartAndTomorrow == 0 && midnightNextDay .getDayOfWeek() <= new DateMidnight(schedule.getBeginDate()).getDayOfWeek())) { // we crossed a week boundary, so add one week. return midnightNextDay.plusWeeks(schedule.getRepeatRate() - 1); } return midnightNextDay; } else if (distanceBetweenStartAndTomorrow > schedule.getRepeatRate()) { int remainder = distanceBetweenStartAndTomorrow % schedule.getRepeatRate(); return midnightNextDay.plusWeeks(schedule.getRepeatRate() - remainder); } else { return midnightNextDay.plusWeeks(schedule.getRepeatRate() - distanceBetweenStartAndTomorrow); } }
From source file:com.google.android.apps.paco.TimeUtil.java
License:Open Source License
public static boolean isWeekend(DateTime dateTime) { return isWeekend(dateTime.getDayOfWeek()); }
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); }/*from w w w . j a v a2 s .com*/ return plusDays; }
From source file:com.google.api.ads.adwords.awreporting.model.entities.dateranges.LastWeekDateRangeHandler.java
License:Open Source License
@Override public DateTime retrieveDateStart(DateTime date) { return date.minusWeeks(1).minusDays(date.getDayOfWeek() - 1); }
From source file:com.google.api.ads.adwords.awreporting.model.entities.dateranges.LastWeekDateRangeHandler.java
License:Open Source License
@Override public DateTime retrieveDateEnd(DateTime date) { return date.minusWeeks(1).plusDays(7 - date.getDayOfWeek()); }
From source file:com.guavabot.alarmpreference.Alarm.java
License:Apache License
/** * Calculates the next time that the alarm should go off. * /*from ww w.j a v a 2 s . com*/ * @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.inkubator.common.util.DateTimeUtil.java
/** * get total SaturDay and Monday between two date * * @param startDate Date reference/*from ww w .j ava 2 s.c om*/ * @return Integer * @param endDate Date reference * @throws java.lang.Exception */ public static Integer getTotalSaturdayAndMonday(Date startDate, Date endDate) throws Exception { if (startDate.after(endDate)) { throw new Exception("Mr. DHFR say :End Date must be newer than Start Date"); } else { DateTime start = new DateTime(startDate); DateTime end = new DateTime(endDate); int totalSaturdayAndMonday = 0; DateTime iterate = start; while (iterate.isBefore(end) | iterate.isEqual(end)) { if (iterate.getDayOfWeek() == 6 | iterate.getDayOfWeek() == 7) { ++totalSaturdayAndMonday; } iterate = iterate.plusDays(1); } return totalSaturdayAndMonday; } }