Example usage for org.joda.time DateMidnight withDayOfWeek

List of usage examples for org.joda.time DateMidnight withDayOfWeek

Introduction

In this page you can find the example usage for org.joda.time DateMidnight withDayOfWeek.

Prototype

public DateMidnight withDayOfWeek(int dayOfWeek) 

Source Link

Document

Returns a copy of this date with the day of week field updated.

Usage

From source file:dk.teachus.backend.domain.impl.PeriodsImpl.java

License:Apache License

public List<DatePeriod> generateDatesForWeek(DateMidnight startDate) {
    List<DatePeriod> dates = new ArrayList<DatePeriod>();
    DateMidnight sd = startDate.withDayOfWeek(DateTimeConstants.MONDAY);
    int week = sd.getWeekOfWeekyear();

    while (week == sd.getWeekOfWeekyear()) {
        DatePeriod datePeriod = null;/*  ww w . j a  va 2s .co m*/
        for (Period period : getValidPeriods()) {
            // Check if this period can handle the date at all
            if (period.dateIntervalContains(sd)) {
                DateMidnight date = period.generateDate(sd);
                if (date != null) {
                    if (datePeriod == null) {
                        datePeriod = new DatePeriodImpl(date);
                        dates.add(datePeriod);
                    }

                    datePeriod.addPeriod(period);
                }
            }
        }

        sd = sd.plusDays(1);
    }

    Collections.sort(dates, new Comparator<DatePeriod>() {
        public int compare(DatePeriod o1, DatePeriod o2) {
            return o1.getDate().compareTo(o2.getDate());
        }
    });

    return dates;
}

From source file:dk.teachus.backend.domain.impl.PeriodsImpl.java

License:Apache License

public List<DatePeriod> generateDates(DateMidnight weekDate, int numberOfDays, boolean explicitNumberOfDays) {
    weekDate = weekDate.withDayOfWeek(DateTimeConstants.MONDAY);

    List<DatePeriod> dates = new ArrayList<DatePeriod>();
    List<DatePeriod> weekDates = generateDatesForWeek(weekDate);
    if (numberOfDays > 0) {
        do {//  ww w .  j  a  v a2 s .  co m
            for (DatePeriod datePeriod : weekDates) {
                dates.add(datePeriod);

                if (explicitNumberOfDays) {
                    if (dates.size() >= numberOfDays) {
                        break;
                    }
                }
            }
            weekDate = weekDate.plusWeeks(1);
            weekDates = generateDatesForWeek(weekDate);
        } while (dates.size() + weekDates.size() <= numberOfDays && hasPeriodAfter(weekDate));
    }

    return dates;
}

From source file:dk.teachus.backend.testdatagenerator.DynamicDataImport.java

License:Apache License

private void createBookings(Teacher teacher, DateMidnight startDate, DateMidnight endDate) {
    Session session = sessionFactory.openSession();
    session.beginTransaction();//from   w  w  w .j a  v a  2s  . c  o m

    Set<DateTime> bookedDates = new HashSet<DateTime>();

    List<Pupil> pupils = getPupils(teacher, session);
    List<Period> periods = getPeriods(teacher, session);

    DateTime now = new DateTime();

    for (Pupil pupil : pupils) {
        DateMidnight date = startDate;
        while (date.isBefore(endDate)) {
            // First see if the pupil should have a booking here at all
            long shouldBook = Math.round(Math.random());
            if (shouldBook == 1) {
                // Then find out which period to book in
                int periodIndex = (int) (Math.random() * periods.size());
                Period period = periods.get(periodIndex);
                log.debug("Booking lesson for period: " + period.getName());

                // Then find out which day in the period to book in
                int weekDayIndex = (int) (Math.random() * period.getWeekDays().size());
                WeekDay weekDay = period.getWeekDays().get(weekDayIndex);
                DateMidnight weekDayDate = date.withDayOfWeek(weekDay.getYodaWeekDay());

                // Now find out which time of day to use
                // We do that by listing the possible booking time entries. A lesson doesn't have to start at a whole number.
                List<DateTime> avaiableLessonsStart = new ArrayList<DateTime>();
                DateTime bookTime = period.getStartTime().toDateTime(weekDayDate);
                DateTime et = period.getEndTime().toDateTime(weekDayDate);
                while (bookTime.isBefore(et)) {
                    if (period.mayBook(bookTime)) {
                        avaiableLessonsStart.add(bookTime);
                    }

                    bookTime = bookTime.plusMinutes(period.getIntervalBetweenLessonStart());
                }

                int selectedIndex = (int) Math.round(Math.random() * (avaiableLessonsStart.size() - 1));
                log.debug("Selected lesson number " + selectedIndex + " out of " + avaiableLessonsStart.size());
                bookTime = avaiableLessonsStart.get(selectedIndex);

                if (bookedDates.contains(bookTime) == false) {
                    bookedDates.add(bookTime);

                    // Create booking
                    PupilBooking booking = new PupilBookingImpl();
                    booking.setCreateDate(new DateTime());
                    booking.setDate(bookTime);
                    booking.setPeriod(period);
                    booking.setTeacher(teacher);
                    booking.setPupil(pupil);
                    booking.setNotificationSent(true);
                    booking.setPupilNotificationSent(true);

                    // Set the paid based on the booking time is in the past
                    if (now.isAfter(bookTime)) {
                        // If it's within a month, then give it a 40% change of not have been paid
                        if (now.minusMonths(1).isBefore(bookTime)) {
                            int pct = (int) (Math.random() * 100.0);
                            if (pct >= 40) {
                                booking.setPaid(true);
                            }
                        } else {
                            booking.setPaid(true);
                        }
                    }

                    // Book
                    session.save(booking);
                }
            }

            date = date.plusWeeks(1);
        }
    }

    session.getTransaction().commit();
    session.close();
}

From source file:org.jasig.portlet.courses.mvc.wrapper.CourseSectionWrapper.java

License:Apache License

@JsonIgnore
public int getExamStartSunday() {
    if (this.getStartDateObj() == null) {
        return 0;
    }//from w  w w . j  a va 2s  . co  m

    DateMidnight date = this.getStartDateObj();

    if (date.getDayOfWeek() == DateTimeConstants.SUNDAY) {
        return date.getDayOfMonth();
    }

    return date.withDayOfWeek(DateTimeConstants.SUNDAY).minusWeeks(1).getDayOfMonth();
}

From source file:org.wicketstuff.ddcalendar.CalendarWeek.java

License:Apache License

public DateMidnight getFirstDay() {
    DateMidnight date = new DateMidnight(year, 1, 1);
    date = date.withWeekOfWeekyear(week);
    date = date.withDayOfWeek(1);
    return date;// w  w w.ja  v a2s .com
}

From source file:org.wicketstuff.ddcalendar.CalendarWeek.java

License:Apache License

public DateMidnight getLastDay() {
    DateMidnight date = new DateMidnight(year, 1, 1);
    date = date.withWeekOfWeekyear(week);
    date = date.withDayOfWeek(7);
    return date;//from w  w w .j  a va  2 s .c  o m
}