Example usage for org.joda.time LocalDateTime getDayOfWeek

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

Introduction

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

Prototype

public int getDayOfWeek() 

Source Link

Document

Get the day of week field value.

Usage

From source file:ca.ualberta.cs.shoven_habittracker.MainActivity.java

License:Creative Commons License

public Integer setDateToday(TextView textView) {
    LocalDateTime now = new LocalDateTime(DateTimeZone.forID("Canada/Mountain"));
    textView.setText(now.toString("EEEE, MMMM dd, yyyy", Locale.CANADA));
    return now.getDayOfWeek() % 7;
}

From source file:com.axelor.apps.crm.service.EventService.java

License:Open Source License

@Transactional
public void addRecurrentEventsByWeeks(Event event, int periodicity, int endType, int repetitionsNumber,
        LocalDate endDate, Map<Integer, Boolean> daysCheckedMap) {
    Event lastEvent = event;//from w  w w . j av  a 2s  .com
    List<Integer> list = new ArrayList<Integer>();
    for (int day : daysCheckedMap.keySet()) {
        list.add(day);
    }
    Collections.sort(list);
    if (endType == 1) {
        int repeated = 0;
        Event copy = eventRepo.copy(lastEvent, false);
        copy.setParentEvent(lastEvent);
        int dayOfWeek = copy.getStartDateTime().getDayOfWeek();
        LocalDateTime nextDateTime = new LocalDateTime();
        if (dayOfWeek < list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
        } else if (dayOfWeek > list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
        }
        Duration dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

        for (Integer integer : list) {
            nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
            copy.setStartDateTime(nextDateTime);
            copy.setEndDateTime(nextDateTime.plus(dur));
            eventRepo.save(copy);
            lastEvent = copy;
            repeated++;
        }

        while (repeated < repetitionsNumber) {
            copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(lastEvent);
            copy.setStartDateTime(copy.getStartDateTime().plusWeeks(periodicity));
            copy.setEndDateTime(copy.getEndDateTime().plusWeeks(periodicity));

            dayOfWeek = copy.getStartDateTime().getDayOfWeek();
            nextDateTime = new LocalDateTime();
            if (dayOfWeek < list.get(0)) {
                nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
            } else if (dayOfWeek > list.get(0)) {
                nextDateTime = new LocalDateTime(
                        copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
            }
            dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

            for (Integer integer : list) {
                nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;
                repeated++;
            }
        }
    } else {

        Event copy = eventRepo.copy(lastEvent, false);
        copy.setParentEvent(lastEvent);
        int dayOfWeek = copy.getStartDateTime().getDayOfWeek();
        LocalDateTime nextDateTime = new LocalDateTime();
        if (dayOfWeek < list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
        } else if (dayOfWeek > list.get(0)) {
            nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
        }
        Duration dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

        for (Integer integer : list) {
            nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
            copy.setStartDateTime(nextDateTime);
            copy.setEndDateTime(nextDateTime.plus(dur));
            eventRepo.save(copy);
            lastEvent = copy;
        }

        while (!copy.getStartDateTime().plusWeeks(periodicity).isAfter(endDate)) {
            copy = eventRepo.copy(lastEvent, false);
            copy.setParentEvent(lastEvent);
            copy.setStartDateTime(copy.getStartDateTime().plusWeeks(periodicity));
            copy.setEndDateTime(copy.getEndDateTime().plusWeeks(periodicity));

            dayOfWeek = copy.getStartDateTime().getDayOfWeek();
            nextDateTime = new LocalDateTime();
            if (dayOfWeek < list.get(0)) {
                nextDateTime = new LocalDateTime(copy.getStartDateTime().plusDays(list.get(0) - dayOfWeek));
            } else if (dayOfWeek > list.get(0)) {
                nextDateTime = new LocalDateTime(
                        copy.getStartDateTime().plusDays((7 - dayOfWeek) + list.get(0)));
            }
            dur = new Duration(copy.getStartDateTime().toDateTime(), copy.getEndDateTime().toDateTime());

            for (Integer integer : list) {
                nextDateTime.plusDays(integer - nextDateTime.getDayOfWeek());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;
            }
        }
    }
}

From source file:com.axelor.apps.crm.service.EventService.java

License:Open Source License

@Transactional
public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber,
        LocalDate endDate, int monthRepeatType) {
    Event lastEvent = event;/*w  w w  . j av  a  2 s .  co m*/
    if (monthRepeatType == 1) {
        int dayOfMonth = event.getStartDateTime().getDayOfMonth();
        if (endType == 1) {
            int repeated = 0;
            while (repeated != repetitionsNumber) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth()
                        .getMaximumValue() >= dayOfMonth) {
                    copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity));
                    copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity));
                    eventRepo.save(copy);
                    repeated++;
                    lastEvent = copy;
                }
            }
        } else {
            while (!lastEvent.getStartDateTime().plusMonths(periodicity).isAfter(endDate)) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                if (copy.getStartDateTime().plusMonths(periodicity).dayOfMonth()
                        .getMaximumValue() >= dayOfMonth) {
                    copy.setStartDateTime(copy.getStartDateTime().plusMonths(periodicity));
                    copy.setEndDateTime(copy.getEndDateTime().plusMonths(periodicity));
                    eventRepo.save(copy);
                    lastEvent = copy;
                }
            }
        }
    }

    else {
        int dayOfWeek = event.getStartDateTime().getDayOfWeek();
        int positionInMonth = 0;
        if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
            positionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
        } else {
            positionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
        }

        if (endType == 1) {
            int repeated = 0;
            while (repeated != repetitionsNumber) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);
                LocalDateTime nextDateTime = new LocalDateTime(copy.getStartDateTime());
                nextDateTime.plusMonths(periodicity);
                int nextDayOfWeek = nextDateTime.getDayOfWeek();
                if (nextDayOfWeek > dayOfWeek) {
                    nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
                } else {
                    nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
                }
                int nextPositionInMonth = 0;
                if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                    nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
                } else {
                    nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
                }
                if (nextPositionInMonth > positionInMonth) {
                    nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
                } else {
                    nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
                }
                Duration dur = new Duration(copy.getStartDateTime().toDateTime(),
                        copy.getEndDateTime().toDateTime());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                repeated++;
                lastEvent = copy;
            }
        } else {
            LocalDateTime nextDateTime = new LocalDateTime(lastEvent.getStartDateTime());
            nextDateTime.plusMonths(periodicity);
            int nextDayOfWeek = nextDateTime.getDayOfWeek();
            if (nextDayOfWeek > dayOfWeek) {
                nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
            } else {
                nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
            }
            int nextPositionInMonth = 0;
            if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
            } else {
                nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
            }
            if (nextPositionInMonth > positionInMonth) {
                nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
            } else {
                nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
            }
            while (!nextDateTime.isAfter(endDate)) {
                Event copy = eventRepo.copy(lastEvent, false);
                copy.setParentEvent(lastEvent);

                Duration dur = new Duration(copy.getStartDateTime().toDateTime(),
                        copy.getEndDateTime().toDateTime());
                copy.setStartDateTime(nextDateTime);
                copy.setEndDateTime(nextDateTime.plus(dur));
                eventRepo.save(copy);
                lastEvent = copy;

                nextDateTime = new LocalDateTime(lastEvent.getStartDateTime());
                nextDateTime.plusMonths(periodicity);
                nextDayOfWeek = nextDateTime.getDayOfWeek();
                if (nextDayOfWeek > dayOfWeek) {
                    nextDateTime.minusDays(nextDayOfWeek - dayOfWeek);
                } else {
                    nextDateTime.plusDays(dayOfWeek - nextDayOfWeek);
                }
                nextPositionInMonth = 0;
                if (event.getStartDateTime().getDayOfMonth() % 7 == 0) {
                    nextPositionInMonth = event.getStartDateTime().getDayOfMonth() / 7;
                } else {
                    nextPositionInMonth = (event.getStartDateTime().getDayOfMonth() / 7) + 1;
                }
                if (nextPositionInMonth > positionInMonth) {
                    nextDateTime.minusWeeks(nextPositionInMonth - positionInMonth);
                } else {
                    nextDateTime.plusWeeks(positionInMonth - nextPositionInMonth);
                }
            }
        }
    }
}

From source file:com.battlelancer.seriesguide.util.TimeTools.java

License:Apache License

/**
 * Calculates the current release date time. Adjusts for time zone effects on release time, e.g.
 * delays between time zones (e.g. in the United States) and DST. Adjusts for user-defined
 * offset.//ww  w .j  av a2s.  c  o m
 *
 * @param time See {@link #getShowReleaseTime(int)}.
 * @return The date is today or on the next day matching the given week day.
 */
public static Date getShowReleaseDateTime(@NonNull Context context, @NonNull LocalTime time, int weekDay,
        @Nullable String timeZone, @Nullable String country) {
    // determine show time zone (falls back to America/New_York)
    DateTimeZone showTimeZone = getDateTimeZone(timeZone);

    // create current date in show time zone, set local show release time
    LocalDateTime localDateTime = new LocalDate(showTimeZone).toLocalDateTime(time);

    // adjust day of week so datetime is today or within the next week
    // for daily shows (weekDay == 0) just use the current day
    if (weekDay >= 1 && weekDay <= 7) {
        // joda tries to preserve week
        // so if we want a week day earlier in the week, advance by 7 days first
        if (weekDay < localDateTime.getDayOfWeek()) {
            localDateTime = localDateTime.plusWeeks(1);
        }
        localDateTime = localDateTime.withDayOfWeek(weekDay);
    }

    localDateTime = handleHourPastMidnight(country, localDateTime);
    localDateTime = handleDstGap(showTimeZone, localDateTime);

    DateTime dateTime = localDateTime.toDateTime(showTimeZone);

    // handle time zone effects on release time for US shows (only if device is set to US zone)
    String localTimeZone = TimeZone.getDefault().getID();
    if (localTimeZone.startsWith(TIMEZONE_ID_PREFIX_AMERICA)) {
        dateTime = applyUnitedStatesCorrections(country, localTimeZone, dateTime);
    }

    dateTime = applyUserOffset(context, dateTime);

    return dateTime.toDate();
}

From source file:com.ephemeraldreams.gallyshuttle.ui.MainFragment.java

License:Apache License

private void setScheduleId() {
    LocalDateTime now = LocalDateTime.now();
    int hour = now.getHourOfDay();
    int minute = now.getMinuteOfHour();
    if (hour >= 21 || (hour == 0 && minute <= 15)) {
        scheduleId = R.array.late_night_stations;
    } else {/*from   ww w . j a v  a 2 s.  c o  m*/
        int day = now.getDayOfWeek();
        switch (day) {
        case SATURDAY:
        case SUNDAY:
            scheduleId = R.array.weekend_stations;
            break;
        default:
            scheduleId = R.array.continuous_stations;
            break;
        }
    }
}

From source file:com.google.gerrit.server.config.ScheduleConfig.java

License:Apache License

private static long initialDelay(Config rc, String section, String subsection, String keyStartTime,
        DateTime now, long interval) {
    long delay = MISSING_CONFIG;
    String start = rc.getString(section, subsection, keyStartTime);
    try {//www .  ja v  a 2s. co  m
        if (start != null) {
            DateTimeFormatter formatter;
            MutableDateTime startTime = now.toMutableDateTime();
            try {
                formatter = ISODateTimeFormat.hourMinute();
                LocalTime firstStartTime = formatter.parseLocalTime(start);
                startTime.hourOfDay().set(firstStartTime.getHourOfDay());
                startTime.minuteOfHour().set(firstStartTime.getMinuteOfHour());
            } catch (IllegalArgumentException e1) {
                formatter = DateTimeFormat.forPattern("E HH:mm").withLocale(Locale.US);
                LocalDateTime firstStartDateTime = formatter.parseLocalDateTime(start);
                startTime.dayOfWeek().set(firstStartDateTime.getDayOfWeek());
                startTime.hourOfDay().set(firstStartDateTime.getHourOfDay());
                startTime.minuteOfHour().set(firstStartDateTime.getMinuteOfHour());
            }
            startTime.secondOfMinute().set(0);
            startTime.millisOfSecond().set(0);
            long s = startTime.getMillis();
            long n = now.getMillis();
            delay = (s - n) % interval;
            if (delay <= 0) {
                delay += interval;
            }
        } else {
            log.info(MessageFormat.format("{0} schedule parameter \"{0}.{1}\" is not configured", section,
                    keyStartTime));
        }
    } catch (IllegalArgumentException e2) {
        log.error(MessageFormat.format("Invalid {0} schedule parameter \"{0}.{1}\"", section, keyStartTime),
                e2);
        delay = INVALID_CONFIG;
    }
    return delay;
}

From source file:com.kccomy.orgar.util.DateFormatUtil.java

License:Apache License

public static String getFormattedDate(Date date) {
    LocalDateTime modified = LocalDateTime.fromDateFields(date);
    LocalDateTime now = LocalDateTime.now();

    if (modified.getWeekOfWeekyear() == now.getWeekOfWeekyear() && modified.getYear() == now.getYear()) {

        if (modified.getDayOfWeek() == now.getDayOfWeek()) {
            return modified.toString(TIME);
        } else {//from w  w w .ja v  a  2 s.  c  om
            return modified.toString(WEEK);
        }
    } else {
        return modified.toString(DATE);
    }
}

From source file:com.tasomaniac.muzei.tvshows.util.TimeTools.java

License:Apache License

/**
 * Calculates the current release date time. Adjusts for time zone effects on release time, e.g.
 * delays between time zones (e.g. in the United States) and DST. Adjusts for user-defined
 * offset.//ww  w  . j a v a2  s .co m
 *
 * @param time See {@link #getShowReleaseTime(int)}.
 * @return The date is today or on the next day matching the given week day.
 */
public static Date getShowReleaseDateTime(@NonNull Context context, @NonNull LocalTime time, int weekDay,
        @Nullable String timeZone, @Nullable String country) {
    // determine show time zone (falls back to America/New_York)
    DateTimeZone showTimeZone = getDateTimeZone(timeZone);

    // create current date in show time zone, set local show release time
    LocalDateTime localDateTime = new LocalDate(showTimeZone).toLocalDateTime(time);

    // adjust day of week so datetime is today or within the next week
    // for daily shows (weekDay == 0) just use the current day
    if (weekDay >= 1 && weekDay <= 7) {
        // joda tries to preserve week
        // so if we want a week day earlier in the week, advance by 7 days first
        if (weekDay < localDateTime.getDayOfWeek()) {
            localDateTime = localDateTime.plusWeeks(1);
        }
        localDateTime = localDateTime.withDayOfWeek(weekDay);
    }

    localDateTime = handleHourPastMidnight(country, localDateTime);
    localDateTime = handleDstGap(showTimeZone, localDateTime);

    DateTime dateTime = localDateTime.toDateTime(showTimeZone);

    // handle time zone effects on release time for US shows (only if device is set to US zone)
    String localTimeZone = TimeZone.getDefault().getID();
    if (localTimeZone.startsWith(TIMEZONE_ID_PREFIX_AMERICA)) {
        dateTime = applyUnitedStatesCorrections(country, localTimeZone, dateTime);
    }

    return dateTime.toDate();
}

From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.SemLeidmiseAbimeetodid.java

License:Open Source License

/**
 *   Rakendab yldistatud Baldwini akent, et leida hetkele <tt>currentDateTime</tt> l2himat 
 *  ajahetke, mis vastab tingimustele <tt>field == soughtValue</tt>. Kui tingimustele vastav
 *  hetk j22b v2lja Baldwini akna raamidest, toimib kui tavaline SET operatsioon, omistades
 *  <tt>field := soughtValue</tt> ajahetke <tt>currentDateTime</tt> raames.
 *  <p>/*from  ww  w.j  a v  a 2s.  c  om*/
 *  Praegu on implementeeritud ainult granulaarsuste 
 *  <tt>DAY_OF_WEEK</tt>, <tt>MONTH</tt>, <tt>YEAR_OF_CENTURY</tt>  
 *  toetus. 
    *  <p>
 *  <i>What's the Date? High Accuracy Interpretation of Weekday Name,</i> Dale, Mazur (2009)
 */
public static LocalDateTime applyBaldwinWindow(Granulaarsus field, LocalDateTime currentDateTime,
        int soughtValue) {
    // ---------------------------------
    //  DAY_OF_WEEK
    // ---------------------------------      
    if (field == Granulaarsus.DAY_OF_WEEK && DateTimeConstants.MONDAY <= soughtValue
            && soughtValue <= DateTimeConstants.SUNDAY) {
        int currentDayOfWeek = currentDateTime.getDayOfWeek();
        int addToCurrent = 0;
        // 1) Vaatame eelnevat 3-e p&auml;eva
        while (addToCurrent > -4) {
            if (currentDayOfWeek == soughtValue) {
                return currentDateTime.plusDays(addToCurrent);
            }
            currentDayOfWeek--;
            if (currentDayOfWeek < DateTimeConstants.MONDAY) {
                currentDayOfWeek = DateTimeConstants.SUNDAY;
            }
            addToCurrent--;
        }
        // 2) Vaatame jargnevat 3-e p&auml;eva
        currentDayOfWeek = currentDateTime.getDayOfWeek();
        addToCurrent = 0;
        while (addToCurrent < 4) {
            if (currentDayOfWeek == soughtValue) {
                return currentDateTime.plusDays(addToCurrent);
            }
            currentDayOfWeek++;
            if (currentDayOfWeek > DateTimeConstants.SUNDAY) {
                currentDayOfWeek = DateTimeConstants.MONDAY;
            }
            addToCurrent++;
        }
    }
    // ---------------------------------
    //  MONTH
    // ---------------------------------
    if (field == Granulaarsus.MONTH && DateTimeConstants.JANUARY <= soughtValue
            && soughtValue <= DateTimeConstants.DECEMBER) {
        int currentMonth = currentDateTime.getMonthOfYear();
        int addToCurrent = 0;
        // 1) Vaatame eelnevat 5-e kuud
        while (addToCurrent > -6) {
            if (currentMonth == soughtValue) {
                return currentDateTime.plusMonths(addToCurrent);
            }
            currentMonth--;
            if (currentMonth < DateTimeConstants.JANUARY) {
                currentMonth = DateTimeConstants.DECEMBER;
            }
            addToCurrent--;
        }
        // 2) Vaatame jargnevat 5-e kuud
        currentMonth = currentDateTime.getMonthOfYear();
        addToCurrent = 0;
        while (addToCurrent < 6) {
            if (currentMonth == soughtValue) {
                return currentDateTime.plusMonths(addToCurrent);
            }
            currentMonth++;
            if (currentMonth > DateTimeConstants.DECEMBER) {
                currentMonth = DateTimeConstants.JANUARY;
            }
            addToCurrent++;
        }
        // Kui otsitav kuu j2i aknast v2lja, k2sitleme seda kui "selle aasta" otsitud kuud
        return currentDateTime.withMonthOfYear(soughtValue);
    }
    // ---------------------------------
    //  YEAR_OF_CENTURY
    // ---------------------------------
    if (field == Granulaarsus.YEAR_OF_CENTURY && 0 <= soughtValue && soughtValue <= 99) {
        // API tunnistab vrtuseid vahemikust 1 kuni 100
        if (soughtValue == 0) {
            soughtValue = 100;
        }
        int currentYear = currentDateTime.getYearOfCentury();
        int addToCurrent = 0;
        // 1) Vaatame eelnevat 4-a aastakymmet 
        while (addToCurrent > -49) {
            if (currentYear == soughtValue) {
                return currentDateTime.plusYears(addToCurrent);
            }
            currentYear--;
            if (currentYear < 1) {
                currentYear = 100;
            }
            addToCurrent--;
        }
        // 2) Vaatame jargnevat 4-a aastakymmet
        currentYear = currentDateTime.getYearOfCentury();
        addToCurrent = 0;
        while (addToCurrent < 49) {
            if (currentYear == soughtValue) {
                return currentDateTime.plusYears(addToCurrent);
            }
            currentYear++;
            if (currentYear > 100) {
                currentYear = 1;
            }
            addToCurrent++;
        }
        // Kui otsitav kuu j2i aknast v2lja, k2sitleme seda kui "selle sajandi" otsitud aastat
        return currentDateTime.withYearOfCentury(soughtValue);
    }
    return currentDateTime;
}

From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.SemLeidmiseAbimeetodid.java

License:Open Source License

/**
 *    Leiab <tt>currentDateTime</tt> granulaarsuse <tt>superField</tt> 
 *   <i>n</i>-inda alamosa, mis vastab tingimustele <tt>subField == soughtValueOfSubField</tt>.
 *   <p>//w  ww  .j a va2s  . c o  m
 *   Negatiivsete <i>n</i> vaartuste korral voetakse alamosa "tagantpoolt": vaartus 
 *   <i>n</i> == -1 tahistab <i>viimast</i>, <i>n</i> == -2 tahistab <i>eelviimast</i>
 *   jne alamosa.
 *   <p>
 *   Praegu on defineeritud ainult <i>kuu n-inda nadalapaeva leidmise</i> operatsioon (
 *   <tt>superField == MONTH</tt>, <tt>subField == DAY_OF_WEEK</tt>, <tt>soughtValueOfSubField == a weekdayname</tt> ).
 */
public static LocalDateTime findNthSubpartOfGranularity(Granulaarsus superField, Granulaarsus subField,
        int soughtValueOfSubField, int n, LocalDateTime currentDateTime) {
    if (superField == Granulaarsus.MONTH) {
        // --------------------------------------      
        //  Kuu n-inda nadalapaeva leidmine ...
        // --------------------------------------
        if (subField == Granulaarsus.DAY_OF_WEEK && DateTimeConstants.MONDAY <= soughtValueOfSubField
                && soughtValueOfSubField <= DateTimeConstants.SUNDAY) {
            if (n > 0) {
                //
                // Algoritm:  
                //    http://msdn.microsoft.com/en-us/library/aa227532(VS.60).aspx
                //
                // Kerime kaesoleva kuu esimese kuupaeva peale ...
                LocalDateTime newDate = currentDateTime.withDayOfMonth(1);
                // Leiame esimese otsitud nadalapaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.plusDays(1);
                }
                int currentMonth = newDate.getMonthOfYear();
                newDate = newDate.plusDays((n - 1) * 7);
                if (currentMonth == newDate.getMonthOfYear()) {
                    // Kui kuu j2i kindlalt samaks, tagastame leitud nadalapaeva
                    return newDate;
                }
            } else if (n < 0) {
                // Negatiivsete vaartuste korral otsime lahendust lopust:
                // Kerime kuu viimase vaartuse peale
                LocalDateTime newDate = currentDateTime
                        .withDayOfMonth(currentDateTime.dayOfMonth().getMaximumValue());
                // Leiame viimase otsitud nadalapaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.minusDays(1);
                }
                int currentMonth = newDate.getMonthOfYear();
                newDate = newDate.minusDays(((n * (-1)) - 1) * 7);
                if (currentMonth == newDate.getMonthOfYear()) {
                    // Kui kuu j2i kindlalt samaks, tagastame leitud nadalapaeva
                    return newDate;
                }
            }
        }
        // -------------------------------------------------
        //   Kuu n-inda ndala/ndalavahetuse leidmine ...
        // -------------------------------------------------
        // -------------------------------------------------------------------
        //   Teeme eelduse, et kuu esimene ndal on ndal, mis sisaldab kuu
        //  esimest nadalapaeva {soughtValueOfSubField};
        //   Ning analoogselt, kuu viimane ndal on ndal, mis sisaldab kuu 
        //  viimast nadalapaeva {soughtValueOfSubField};
        // -------------------------------------------------------------------
        if (subField == Granulaarsus.WEEK_OF_YEAR && DateTimeConstants.MONDAY <= soughtValueOfSubField
                && soughtValueOfSubField <= DateTimeConstants.SUNDAY) {
            if (n > 0) {
                // Kerime kaesoleva kuu esimese paeva peale ...
                LocalDateTime newDate = currentDateTime.withDayOfMonth(1);
                // Leiame kuu esimese neljapaeva/laupaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.plusDays(1);
                }
                newDate = newDate.plusDays((n - 1) * 7);
                return newDate;
            } else if (n < 0) {
                // Negatiivsete vaartuste korral otsime lahendust lopust:
                // Kerime kuu viimase vaartuse peale
                LocalDateTime newDate = currentDateTime
                        .withDayOfMonth(currentDateTime.dayOfMonth().getMaximumValue());
                // Leiame viimase neljapaeva/laupaeva
                while (newDate.getDayOfWeek() != soughtValueOfSubField) {
                    newDate = newDate.minusDays(1);
                }
                newDate = newDate.minusDays(((n * (-1)) - 1) * 7);
                return newDate;
            }
        }
    }
    return null;
}