Example usage for org.joda.time LocalDateTime withYearOfCentury

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

Introduction

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

Prototype

public LocalDateTime withYearOfCentury(int yearOfCentury) 

Source Link

Document

Returns a copy of this datetime with the year of century field updated.

Usage

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 av  a  2s  .c o m*/
 *  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;
}