List of usage examples for org.joda.time LocalDate getDayOfWeek
public int getDayOfWeek()
From source file:de.appsolve.padelcampus.utils.BookingUtil.java
public void addWeekView(HttpServletRequest request, LocalDate selectedDate, List<Facility> selectedFacilities, ModelAndView mav, Boolean onlyFutureTimeSlots, Boolean preventOverlapping) throws JsonProcessingException { //calculate date configuration for datepicker LocalDate today = new LocalDate(DEFAULT_TIMEZONE); LocalDate firstDay = today.dayOfMonth().withMinimumValue(); LocalDate lastDay = getLastBookableDay(request).plusDays(1); List<CalendarConfig> calendarConfigs = calendarConfigDAO.findBetween(firstDay, lastDay); Collections.sort(calendarConfigs); Map<String, DatePickerDayConfig> dayConfigs = getDayConfigMap(firstDay, lastDay, calendarConfigs); List<Booking> confirmedBookings = bookingDAO.findBlockedBookingsBetween(firstDay, lastDay); //calculate available time slots List<TimeSlot> timeSlots = new ArrayList<>(); List<LocalDate> weekDays = new ArrayList<>(); for (int i = 1; i <= CalendarWeekDay.values().length; i++) { LocalDate date = selectedDate.withDayOfWeek(i); weekDays.add(date);/*from w w w . ja v a2 s .c om*/ if ((!onlyFutureTimeSlots || !date.isBefore(today)) && lastDay.isAfter(date)) { try { //generate list of bookable time slots timeSlots.addAll(getTimeSlotsForDate(date, calendarConfigs, confirmedBookings, onlyFutureTimeSlots, preventOverlapping)); } catch (CalendarConfigException e) { //safe to ignore } } } SortedSet<Offer> offers = new TreeSet<>(); List<TimeRange> rangeList = new ArrayList<>(); //Map<TimeRange, List<TimeSlot>> rangeList = new TreeMap<>(); for (TimeSlot slot : timeSlots) { Set<Offer> slotOffers = slot.getConfig().getOffers(); offers.addAll(slotOffers); TimeRange range = new TimeRange(); range.setStartTime(slot.getStartTime()); range.setEndTime(slot.getEndTime()); if (rangeList.contains(range)) { range = rangeList.get(rangeList.indexOf(range)); } else { rangeList.add(range); } List<TimeSlot> slotis = range.getTimeSlots(); slotis.add(slot); range.setTimeSlots(slotis); } Collections.sort(rangeList); List<Offer> selectedOffers = new ArrayList<>(); if (selectedFacilities.isEmpty()) { selectedOffers = offerDAO.findAll(); } else { for (Facility facility : selectedFacilities) { selectedOffers.addAll(facility.getOffers()); } } Collections.sort(selectedOffers); mav.addObject("dayConfigs", objectMapper.writeValueAsString(dayConfigs)); mav.addObject("maxDate", lastDay.toString()); mav.addObject("Day", selectedDate); mav.addObject("NextMonday", selectedDate.plusDays(8 - selectedDate.getDayOfWeek())); mav.addObject("PrevSunday", selectedDate.minusDays(selectedDate.getDayOfWeek())); mav.addObject("WeekDays", weekDays); mav.addObject("RangeMap", rangeList); mav.addObject("Offers", offers); mav.addObject("SelectedOffers", selectedOffers); mav.addObject("SelectedFacilities", selectedFacilities); mav.addObject("Facilities", facilityDAO.findAll()); }
From source file:de.appsolve.padelcampus.utils.CalendarConfigUtil.java
public List<CalendarConfig> getCalendarConfigsMatchingDate(List<CalendarConfig> configs, LocalDate date) { List<CalendarConfig> configsMatchingDate = new ArrayList<>(); Iterator<CalendarConfig> iterator = configs.iterator(); while (iterator.hasNext()) { CalendarConfig config = iterator.next(); //remove configurations that do not match the requested week day boolean matchesWeekDay = false; for (CalendarWeekDay weekDay : config.getCalendarWeekDays()) { if (weekDay.ordinal() + 1 == date.getDayOfWeek()) { matchesWeekDay = true;/*from w w w . j av a2 s .c om*/ break; } } if (!matchesWeekDay) { continue; } //remove configurations that are defined holidays String holidayKey = config.getHolidayKey(); boolean isHoliday = false; if (!holidayKey.equals(NO_HOLIDAY_KEY)) { String[] holidayKeySplit = holidayKey.split("-"); String country = holidayKeySplit[0]; String region = holidayKeySplit[1]; HolidayManager countryHolidays = HolidayManager .getInstance(ManagerParameters.create(HolidayCalendar.valueOf(country))); isHoliday = countryHolidays.isHoliday(date, region); } if (isHoliday) { continue; } //remove configurations that start after the requested date if (config.getStartDate().compareTo(date) > 0) { continue; } //remove configurations that end before the requested date if (config.getEndDate().compareTo(date) < 0) { continue; } configsMatchingDate.add(config); } Collections.sort(configsMatchingDate); return configsMatchingDate; }
From source file:de.jollyday.parser.impl.FixedWeekdayBetweenFixedParser.java
License:Apache License
/** * {@inheritDoc}/*from w w w. j a v a2 s.c o m*/ * * Parses the provided configuration and creates holidays for the provided * year. */ @Override public void parse(int year, Set<Holiday> holidays, final Holidays config) { for (FixedWeekdayBetweenFixed fwm : config.getFixedWeekdayBetweenFixed()) { if (!isValid(fwm, year)) { continue; } LocalDate from = calendarUtil.create(year, fwm.getFrom()); LocalDate to = calendarUtil.create(year, fwm.getTo()); LocalDate result = null; for (; !from.isAfter(to);) { if (from.getDayOfWeek() == xmlUtil.getWeekday(fwm.getWeekday())) { result = from; break; } from = from.plusDays(1); } if (result != null) { HolidayType type = xmlUtil.getType(fwm.getLocalizedType()); holidays.add(new Holiday(result, fwm.getDescriptionPropertiesKey(), type)); } } }
From source file:de.jollyday.parser.impl.FixedWeekdayRelativeToFixedParser.java
License:Apache License
private LocalDate moveDateToFirstOccurenceOfWeekday(FixedWeekdayRelativeToFixed f, LocalDate day) { do {/*from w ww. ja v a2 s. c o m*/ // move fixed to first occurrence of weekday day = f.getWhen() == When.AFTER ? day.plusDays(1) : day.minusDays(1); } while (day.getDayOfWeek() != xmlUtil.getWeekday(f.getWeekday())); return day; }
From source file:de.jollyday.parser.impl.RelativeToFixedParser.java
License:Apache License
/** {@inheritDoc} */ @Override//from w w w.j av a 2 s . c om public void parse(int year, Set<Holiday> holidays, final Holidays config) { for (RelativeToFixed rf : config.getRelativeToFixed()) { if (!isValid(rf, year)) { continue; } LocalDate fixed = calendarUtil.create(year, rf.getDate()); if (rf.getWeekday() != null) { // if weekday is set -> move to weekday int day = xmlUtil.getWeekday(rf.getWeekday()); int direction = (rf.getWhen() == When.BEFORE ? -1 : 1); do { fixed = fixed.plusDays(direction); } while (fixed.getDayOfWeek() != day); } else if (rf.getDays() != null) { // if number of days set -> move number of days fixed = fixed.plusDays(rf.getWhen() == When.BEFORE ? -rf.getDays() : rf.getDays()); } HolidayType type = xmlUtil.getType(rf.getLocalizedType()); holidays.add(new Holiday(fixed, rf.getDescriptionPropertiesKey(), type)); } }
From source file:edu.wisc.hr.demo.support.PayPeriodGenerator.java
License:Apache License
/** * Returns a List of pay periods, where pay periods are Strings of the form * "Date - Date", as in "2013-08-02 - 2013-08-16". * @param howMany a positive integer or zero * @return a List of howMany Strings representing pay periods * @throws IllegalArgumentException if howMany < 0 *///w w w . j a v a 2 s . co m public List<String> payPeriods(int howMany) { if (howMany < 0) { throw new IllegalArgumentException("Cannot generate negative number of pay periods."); } // if this method were expensive or frequently called we could cache results since it's the same // result for each call for a given howMany. Indeed, could further optimize since howMany( N + 1) is // howMany( N) with an additional pay period prepended. // // compute cycles are cheap and premature optimization is List<String> payPeriods = new LinkedList<String>(); int daysAgoToStart = ((PERIOD_DURATION_IN_DAYS + 2) * howMany); LocalDate startNextPayPeriod = new LocalDate().minusDays(daysAgoToStart); // TODO: calculate the exact number of days to add once and skip the loop while (startNextPayPeriod.getDayOfWeek() != PERIOD_START_DAY) { startNextPayPeriod = startNextPayPeriod.plusDays(1); } // startNextPayPeriod is now a PERIOD_START_DAY (e.g., Sunday) sufficiently long ago that // we can generate howMany periods from there and not quite hit today for (int i = 0; i < howMany; i++) { LocalDate startThisPayPeriod = startNextPayPeriod; LocalDate endThisPayPeriod = startThisPayPeriod.plusDays(PERIOD_DURATION_IN_DAYS - 1); // TODO: use a format string for better self-respect String payPeriodRepresentation = startThisPayPeriod.toString().concat(" - ") .concat(endThisPayPeriod.toString()); payPeriods.add(payPeriodRepresentation); startNextPayPeriod = endThisPayPeriod.plusDays(1); } return payPeriods; }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.SemLeidmiseAbimeetodid.java
License:Open Source License
/** * Tagastab kuupäevas kajastuvale ajale vastavalt, kas on tegu toopaeva (WD) voi nadalavahetusega (WE). *//* w w w . ja v a 2 s .co m*/ public static String getWordDayOrWeekend(LocalDate date) { int weekDay = date.getDayOfWeek(); if (weekDay == DateTimeConstants.SATURDAY || weekDay == DateTimeConstants.SUNDAY) { return "WE"; } else { return "WD"; } }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
public void seekField(Granulaarsus field, int direction, int soughtValue, boolean excludingCurrent) { // --------------------------------- // DAY_OF_MONTH // --------------------------------- if (field == Granulaarsus.DAY_OF_MONTH && direction != 0 && soughtValue == 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); LocalDate nihutatudFookus = ajaFookus.plusDays(1 * dir); ajaFookus = nihutatudFookus;/*from w w w .j av a2s . c om*/ this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // DAY_OF_WEEK // --------------------------------- if (field == Granulaarsus.DAY_OF_WEEK && soughtValue >= DateTimeConstants.MONDAY && soughtValue <= DateTimeConstants.SUNDAY && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne p2ev ehk p2ev, millest tahame tingimata m66duda int algneNadalapaev = (excludingCurrent) ? (ajaFookus.getDayOfWeek()) : (-1); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale p2evale ajaFookus = ajaFookus.plusDays(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusDays(1 * dir); ajaFookus = nihutatudFookus; int uusNadalapaev = ajaFookus.getDayOfWeek(); if (algneNadalapaev != -1) { if (algneNadalapaev == uusNadalapaev) { continue; } else { algneNadalapaev = -1; } } if (uusNadalapaev == soughtValue) { algneNadalapaev = uusNadalapaev; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // WEEK OF YEAR // --------------------------------- if (field == Granulaarsus.WEEK_OF_YEAR && soughtValue == 0 && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne n2dal ehk n2dal, millest tahame m88duda int algneNadal = (excludingCurrent) ? (ajaFookus.getWeekOfWeekyear()) : (-1); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale p2evale ajaFookus = ajaFookus.plusDays(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusDays(1 * dir); ajaFookus = nihutatudFookus; int uusNadal = nihutatudFookus.getWeekOfWeekyear(); if (algneNadal != -1) { if (algneNadal == uusNadal) { continue; } else { algneNadal = -1; } } if (soughtValue == 0) { algneNadal = uusNadal; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.WEEK_OF_YEAR, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // MONTH // --------------------------------- if (field == Granulaarsus.MONTH && (soughtValue == 0 || DateTimeConstants.JANUARY <= soughtValue && soughtValue <= DateTimeConstants.DECEMBER) && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne kuu ehk kuu, millest tahame m88duda int algneKuu = (excludingCurrent) ? (ajaFookus.getMonthOfYear()) : (-1); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale kuule ajaFookus = ajaFookus.plusMonths(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusMonths(1 * dir); ajaFookus = nihutatudFookus; int uusKuu = nihutatudFookus.getMonthOfYear(); if (algneKuu != -1) { if (algneKuu == uusKuu) { continue; } else { algneKuu = -1; } } if (soughtValue == 0 || (soughtValue != 0 && uusKuu == soughtValue)) { algneKuu = uusKuu; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.MONTH, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // YEAR // --------------------------------- if (field == Granulaarsus.YEAR && soughtValue == 0 && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne aasta ehk aasta, millest tahame m88duda int algneAasta = (excludingCurrent) ? (ajaFookus.getYear()) : (-1); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale kuule ajaFookus = ajaFookus.plusMonths(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusMonths(1 * dir); ajaFookus = nihutatudFookus; int uusAasta = nihutatudFookus.getYear(); if (algneAasta != -1) { if (algneAasta == uusAasta) { continue; } else { algneAasta = -1; } } if (soughtValue == 0) { algneAasta = uusAasta; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.YEAR, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // YEAR_OF_CENTURY // --------------------------------- if (field == Granulaarsus.YEAR_OF_CENTURY && direction != 0) { int minValue = SemLeidmiseAbimeetodid.getLocalDateTimeFieldExtremum(this.underlyingDate, DateTimeFieldType.yearOfCentury(), false); int maxValue = SemLeidmiseAbimeetodid.getLocalDateTimeFieldExtremum(this.underlyingDate, DateTimeFieldType.yearOfCentury(), true); if (minValue <= soughtValue && soughtValue <= maxValue) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne aasta ehk aasta, millest tahame m88duda int algneAasta = (excludingCurrent) ? (ajaFookus.getYearOfCentury()) : (-1); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale aastale ajaFookus = ajaFookus.plusYears(dir * (-1)); } int count = 0; int cycleCount = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusYears(1 * dir); cycleCount++; ajaFookus = nihutatudFookus; int uusAasta = nihutatudFookus.getYearOfCentury(); if (algneAasta != -1) { if (algneAasta == uusAasta) { continue; } else { algneAasta = -1; } } if (uusAasta == soughtValue) { algneAasta = uusAasta; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.YEAR, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } } // --------------------------------- // CENTURY_OF_ERA // --------------------------------- if (field == Granulaarsus.CENTURY_OF_ERA && soughtValue == 0 && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne saj ehk sajand, millest tahame m88duda int algneSajand = (excludingCurrent) ? (ajaFookus.getCenturyOfEra()) : (Integer.MIN_VALUE); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale aastale ajaFookus = ajaFookus.plusYears(dir * (-10)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusYears(10 * dir); ajaFookus = nihutatudFookus; int uusSajand = nihutatudFookus.getCenturyOfEra(); if (algneSajand != Integer.MIN_VALUE) { if (algneSajand == uusSajand) { continue; } else { algneSajand = Integer.MIN_VALUE; } } if (soughtValue == 0) { algneSajand = uusSajand; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; updateDateRepresentation(Granulaarsus.CENTURY_OF_ERA, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } }
From source file:es.usc.citius.servando.calendula.util.PickupUtils.java
License:Open Source License
public Pair<LocalDate, List<PickupInfo>> getBestDay() { if (this.bestDay != null) { return this.bestDay; }// w w w.j a v a2 s. c o m HashMap<LocalDate, List<PickupInfo>> bestDays = new HashMap<>(); if (pickups.size() > 0) { LocalDate today = LocalDate.now(); LocalDate first = LocalDate.now(); LocalDate now = LocalDate.now().minusDays(MAX_DAYS); if (now.getDayOfWeek() == DateTimeConstants.SUNDAY) { now = now.plusDays(1); } // get the date of the first med we can take from 10 days ago for (PickupInfo p : pickups) { if (p.from().isAfter(now) && !p.taken()) { first = p.from(); break; } } for (int i = 0; i < 10; i++) { LocalDate d = first.plusDays(i); if (!d.isAfter(today) && d.getDayOfWeek() != DateTimeConstants.SUNDAY) { // only take care of days after today that are not sundays continue; } // compute the number of meds we cant take for each day for (PickupInfo p : pickups) { // get the pickup take secure interval DateTime iStart = p.from().toDateTimeAtStartOfDay(); DateTime iEnd = p.from().plusDays(MAX_DAYS - 1).toDateTimeAtStartOfDay(); Interval interval = new Interval(iStart, iEnd); // add the pickup to the daily list if we can take it if (!p.taken() && interval.contains(d.toDateTimeAtStartOfDay())) { if (!bestDays.containsKey(d)) { bestDays.put(d, new ArrayList<PickupInfo>()); } bestDays.get(d).add(p); } } } // select the day with the highest number of meds int bestDayCount = 0; LocalDate bestOption = null; Set<LocalDate> localDates = bestDays.keySet(); ArrayList<LocalDate> sorted = new ArrayList<>(localDates); Collections.sort(sorted); for (LocalDate day : sorted) { List<PickupInfo> pks = bestDays.get(day); Log.d("PickupUtils", day.toString("dd/MM/YYYY") + ": " + pks.size()); if (pks.size() >= bestDayCount) { bestDayCount = pks.size(); bestOption = day; if (bestOption.getDayOfWeek() == DateTimeConstants.SUNDAY) { bestOption = bestOption.minusDays(1); } } } if (bestOption != null) { this.bestDay = new Pair<>(bestOption, bestDays.get(bestOption)); return this.bestDay; } } return null; }
From source file:me.vertretungsplan.parser.WebUntisParser.java
License:Mozilla Public License
private void parseTimetable(JSONArray json, SubstitutionSchedule schedule, TimeGrid timegrid) throws JSONException, CredentialInvalidException { for (int i = 0; i < json.length(); i++) { JSONObject lesson = json.getJSONObject(i); if (!lesson.has("code") && !lesson.has("substText")) continue; Substitution subst = new Substitution(); if (lesson.has("code")) { subst.setType(codeToType(lesson.getString("code"))); } else {/*from w w w . j av a 2s . c o m*/ subst.setType("Vertretung"); } subst.setColor(colorProvider.getColor(subst.getType())); parseClasses(lesson, subst); parseSubjects(lesson, subst); parseRooms(lesson, subst); parseTeachers(schedule, lesson, subst); if (lesson.has("lstext") && lesson.has("substText") && !lesson.getString("lstext").equals(lesson.getString("substText"))) { subst.setDesc(lesson.getString("lstext") + ", " + lesson.getString("substText")); } else if (lesson.has("lstext")) { subst.setDesc(lesson.getString("lstext")); } else if (lesson.has("substText")) { subst.setDesc(lesson.getString("substText")); } LocalDate date = DATE_FORMAT.parseLocalDate(String.valueOf(lesson.getInt("date"))); LocalTime start = TIME_FORMAT.parseLocalTime(getParseableTime(lesson.getInt("startTime"))); LocalTime end = TIME_FORMAT.parseLocalTime(getParseableTime(lesson.getInt("endTime"))); TimeGrid.Day timegridDay = timegrid.getDay(date.getDayOfWeek()); subst.setLesson(timegridDay != null ? timegridDay.getLesson(start, end) : ""); SubstitutionScheduleDay day = getDayForDate(schedule, date); day.addSubstitution(subst); } }