Java Code Examples for org.joda.time.DateTimeConstants#MILLIS_PER_WEEK

The following examples show how to use org.joda.time.DateTimeConstants#MILLIS_PER_WEEK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: 1_BasicChronology.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
     * @param instant millis from 1970-01-01T00:00:00Z
     * @param year precalculated year of millis
     */
    int getWeekOfWeekyear(long instant, int year) {
        long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
// start of generated patch
if(instant<firstWeekMillis1){
return (int)(instant%DateTimeConstants.MILLIS_PER_DAY);
}
// end of generated patch
/* start of original code
        if (instant < firstWeekMillis1) {
            return getWeeksInYear(year - 1);
        }
 end of original code*/
        long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
        if (instant >= firstWeekMillis2) {
            return 1;
        }
        return (int) ((instant - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK) + 1;
    }
 
Example 2
Source File: BasicWeekyearDateTimeField.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
    if (minuendInstant < subtrahendInstant) {
        return -getDifference(subtrahendInstant, minuendInstant);
    }

    int minuendWeekyear = get(minuendInstant);
    int subtrahendWeekyear = get(subtrahendInstant);

    long minuendRem = remainder(minuendInstant);
    long subtrahendRem = remainder(subtrahendInstant);

    // Balance leap weekyear differences on remainders.
    if (subtrahendRem >= WEEK_53 && iChronology.getWeeksInYear(minuendWeekyear) <= 52) {
        subtrahendRem -= DateTimeConstants.MILLIS_PER_WEEK;
    }

    int difference = minuendWeekyear - subtrahendWeekyear;
    if (minuendRem < subtrahendRem) {
        difference--;
    }
    return difference;
}
 
Example 3
Source File: BasicWeekyearDateTimeField.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
    if (minuendInstant < subtrahendInstant) {
        return -getDifference(subtrahendInstant, minuendInstant);
    }

    int minuendWeekyear = get(minuendInstant);
    int subtrahendWeekyear = get(subtrahendInstant);

    long minuendRem = remainder(minuendInstant);
    long subtrahendRem = remainder(subtrahendInstant);

    // Balance leap weekyear differences on remainders.
    if (subtrahendRem >= WEEK_53 && iChronology.getWeeksInYear(minuendWeekyear) <= 52) {
        subtrahendRem -= DateTimeConstants.MILLIS_PER_WEEK;
    }

    int difference = minuendWeekyear - subtrahendWeekyear;
    if (minuendRem < subtrahendRem) {
        difference--;
    }
    return difference;
}
 
Example 4
Source File: Nopol2017_0088_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param instant millis from 1970-01-01T00:00:00Z
 * @param year precalculated year of millis
 */
int getWeekOfWeekyear(long instant, int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    if (instant < firstWeekMillis1) {
        return getWeeksInYear(year - 1);
    }
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    if (instant >= firstWeekMillis2) {
        return 1;
    }
    return (int) ((instant - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK) + 1;
}
 
Example 5
Source File: Nopol2017_0088_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param instant millis from 1970-01-01T00:00:00Z
 * @param year precalculated year of millis
 */
int getWeekOfWeekyear(long instant, int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    if (instant < firstWeekMillis1) {
        return getWeeksInYear(year - 1);
    }
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    if (instant >= firstWeekMillis2) {
        return 1;
    }
    return (int) ((instant - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK) + 1;
}
 
Example 6
Source File: BasicWeekyearDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public long roundFloor(long instant) {
    // Note: This works fine, but it ideally shouldn't invoke other
    // fields from within a field.
    instant = iChronology.weekOfWeekyear().roundFloor(instant);
    int wow = iChronology.getWeekOfWeekyear(instant);
    if (wow > 1) {
        instant -= ((long) DateTimeConstants.MILLIS_PER_WEEK) * (wow - 1);
    }
    return instant;
}
 
Example 7
Source File: BasicChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param instant millis from 1970-01-01T00:00:00Z
 * @param year precalculated year of millis
 */
int getWeekOfWeekyear(long instant, int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    if (instant < firstWeekMillis1) {
        return getWeeksInYear(year - 1);
    }
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    if (instant >= firstWeekMillis2) {
        return 1;
    }
    return (int) ((instant - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK) + 1;
}
 
Example 8
Source File: BasicWeekyearDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public long roundFloor(long instant) {
    // Note: This works fine, but it ideally shouldn't invoke other
    // fields from within a field.
    instant = iChronology.weekOfWeekyear().roundFloor(instant);
    int wow = iChronology.getWeekOfWeekyear(instant);
    if (wow > 1) {
        instant -= ((long) DateTimeConstants.MILLIS_PER_WEEK) * (wow - 1);
    }
    return instant;
}
 
Example 9
Source File: BasicChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param instant millis from 1970-01-01T00:00:00Z
 * @param year precalculated year of millis
 */
int getWeekOfWeekyear(long instant, int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    if (instant < firstWeekMillis1) {
        return getWeeksInYear(year - 1);
    }
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    if (instant >= firstWeekMillis2) {
        return 1;
    }
    return (int) ((instant - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK) + 1;
}
 
Example 10
Source File: BasicWeekyearDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Set the Year of a week based year component of the specified time instant.
 *
 * @see org.joda.time.DateTimeField#set
 * @param instant  the time instant in millis to update.
 * @param year  the year (-9999,9999) to set the date to.
 * @return the updated DateTime.
 * @throws IllegalArgumentException  if year is invalid.
 */
public long set(long instant, int year) {
    FieldUtils.verifyValueBounds(this, Math.abs(year),
                                 iChronology.getMinYear(), iChronology.getMaxYear());
    //
    // Do nothing if no real change is requested.
    //
    int thisWeekyear = get( instant );
    if ( thisWeekyear == year ) {
        return instant;
    }
    //
    // Calculate the DayOfWeek (to be preserved).
    //
    int thisDow = iChronology.getDayOfWeek(instant);
    //
    // Calculate the maximum weeks in the target year.
    //
    int weeksInFromYear = iChronology.getWeeksInYear( thisWeekyear );
    int weeksInToYear = iChronology.getWeeksInYear( year );
    int maxOutWeeks = (weeksInToYear < weeksInFromYear) ?
        weeksInToYear : weeksInFromYear;
    //
    // Get the current week of the year. This will be preserved in
    // the output unless it is greater than the maximum possible
    // for the target weekyear.  In that case it is adjusted
    // to the maximum possible.
    //
    int setToWeek = iChronology.getWeekOfWeekyear(instant);
    if ( setToWeek > maxOutWeeks ) {
        setToWeek = maxOutWeeks;
    }
    //
    // Get a wroking copy of the current date-time.
    // This can be a convenience for debugging.
    //
    long workInstant = instant; // Get a copy
    //
    // Attempt to get close to the proper weekyear.
    // Note - we cannot currently call ourself, so we just call
    // set for the year.  This at least gets us close.
    //
    workInstant = iChronology.setYear( workInstant, year );
    //
    // Calculate the weekyear number for the get close to value
    // (which might not be equal to the year just set).
    //
    int workWoyYear = get( workInstant );

    //
    // At most we are off by one year, which can be "fixed" by
    // adding/subtracting a week.
    //
    if ( workWoyYear < year ) {
        workInstant += DateTimeConstants.MILLIS_PER_WEEK;
    } else if ( workWoyYear > year ) {
        workInstant -= DateTimeConstants.MILLIS_PER_WEEK;
    }
    //
    // Set the proper week in the current weekyear.
    //

    // BEGIN: possible set WeekOfWeekyear logic.
    int currentWoyWeek = iChronology.getWeekOfWeekyear(workInstant);
    // No range check required (we already know it is OK).
    workInstant = workInstant + (setToWeek - currentWoyWeek)
        * (long)DateTimeConstants.MILLIS_PER_WEEK;
    // END: possible set WeekOfWeekyear logic.

    //
    // Reset DayOfWeek to previous value.
    //
    // Note: This works fine, but it ideally shouldn't invoke other
    // fields from within a field.
    workInstant = iChronology.dayOfWeek().set( workInstant, thisDow );
    //
    // Return result.
    //
    return workInstant;
}
 
Example 11
Source File: BasicWeekyearDateTimeField.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Set the Year of a week based year component of the specified time instant.
 *
 * @see org.joda.time.DateTimeField#set
 * @param instant  the time instant in millis to update.
 * @param year  the year (-9999,9999) to set the date to.
 * @return the updated DateTime.
 * @throws IllegalArgumentException  if year is invalid.
 */
public long set(long instant, int year) {
    FieldUtils.verifyValueBounds(this, Math.abs(year),
                                 iChronology.getMinYear(), iChronology.getMaxYear());
    //
    // Do nothing if no real change is requested.
    //
    int thisWeekyear = get( instant );
    if ( thisWeekyear == year ) {
        return instant;
    }
    //
    // Calculate the DayOfWeek (to be preserved).
    //
    int thisDow = iChronology.getDayOfWeek(instant);
    //
    // Calculate the maximum weeks in the target year.
    //
    int weeksInFromYear = iChronology.getWeeksInYear( thisWeekyear );
    int weeksInToYear = iChronology.getWeeksInYear( year );
    int maxOutWeeks = (weeksInToYear < weeksInFromYear) ?
        weeksInToYear : weeksInFromYear;
    //
    // Get the current week of the year. This will be preserved in
    // the output unless it is greater than the maximum possible
    // for the target weekyear.  In that case it is adjusted
    // to the maximum possible.
    //
    int setToWeek = iChronology.getWeekOfWeekyear(instant);
    if ( setToWeek > maxOutWeeks ) {
        setToWeek = maxOutWeeks;
    }
    //
    // Get a wroking copy of the current date-time.
    // This can be a convenience for debugging.
    //
    long workInstant = instant; // Get a copy
    //
    // Attempt to get close to the proper weekyear.
    // Note - we cannot currently call ourself, so we just call
    // set for the year.  This at least gets us close.
    //
    workInstant = iChronology.setYear( workInstant, year );
    //
    // Calculate the weekyear number for the get close to value
    // (which might not be equal to the year just set).
    //
    int workWoyYear = get( workInstant );

    //
    // At most we are off by one year, which can be "fixed" by
    // adding/subtracting a week.
    //
    if ( workWoyYear < year ) {
        workInstant += DateTimeConstants.MILLIS_PER_WEEK;
    } else if ( workWoyYear > year ) {
        workInstant -= DateTimeConstants.MILLIS_PER_WEEK;
    }
    //
    // Set the proper week in the current weekyear.
    //

    // BEGIN: possible set WeekOfWeekyear logic.
    int currentWoyWeek = iChronology.getWeekOfWeekyear(workInstant);
    // No range check required (we already know it is OK).
    workInstant = workInstant + (setToWeek - currentWoyWeek)
        * (long)DateTimeConstants.MILLIS_PER_WEEK;
    // END: possible set WeekOfWeekyear logic.

    //
    // Reset DayOfWeek to previous value.
    //
    // Note: This works fine, but it ideally shouldn't invoke other
    // fields from within a field.
    workInstant = iChronology.dayOfWeek().set( workInstant, thisDow );
    //
    // Return result.
    //
    return workInstant;
}
 
Example 12
Source File: 1_BasicChronology.java    From SimFix with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the number of weeks in the year.
 *
 * @param year  the year to use
 * @return number of weeks in the year
 */
int getWeeksInYear(int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    return (int) ((firstWeekMillis2 - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK);
}
 
Example 13
Source File: Nopol2017_0088_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Get the number of weeks in the year.
 *
 * @param year  the year to use
 * @return number of weeks in the year
 */
int getWeeksInYear(int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    return (int) ((firstWeekMillis2 - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK);
}
 
Example 14
Source File: Nopol2017_0088_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Get the number of weeks in the year.
 *
 * @param year  the year to use
 * @return number of weeks in the year
 */
int getWeeksInYear(int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    return (int) ((firstWeekMillis2 - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK);
}
 
Example 15
Source File: BasicChronology.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the number of weeks in the year.
 *
 * @param year  the year to use
 * @return number of weeks in the year
 */
int getWeeksInYear(int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    return (int) ((firstWeekMillis2 - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK);
}
 
Example 16
Source File: BasicChronology.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the number of weeks in the year.
 *
 * @param year  the year to use
 * @return number of weeks in the year
 */
int getWeeksInYear(int year) {
    long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
    long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
    return (int) ((firstWeekMillis2 - firstWeekMillis1) / DateTimeConstants.MILLIS_PER_WEEK);
}