Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2013 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.chrono;
 
Provides time calculations for the month of the year component of time.

Author(s):
Guy Allard
Stephen Colebourne
Brian S O'Neill
Since:
1.2, refactored from GJMonthOfYearDateTimeField
 
Serialization version
 
     @SuppressWarnings("unused")
     private static final long serialVersionUID = -8258715387168736L;
 
     private static final int MIN = .;
 
     private final BasicChronology iChronology;
     private final int iMax;
     private final int iLeapMonth;

    
Restricted constructor.

Parameters:
leapMonth the month of year that leaps
 
     BasicMonthOfYearDateTimeField(BasicChronology chronologyint leapMonth) {
         super(DateTimeFieldType.monthOfYear(), chronology.getAverageMillisPerMonth());
          = chronology;
          = .getMaxMonth();
          = leapMonth;
     }
 
     //-----------------------------------------------------------------------
     public boolean isLenient() {
         return false;
     }
 
     //-----------------------------------------------------------------------
     
Get the Month component of the specified time instant.

Parameters:
instant the time instant in millis to query.
Returns:
the month extracted from the input.
See also:
org.joda.time.DateTimeField.get(long)
org.joda.time.ReadableDateTime.getMonthOfYear()
 
     public int get(long instant) {
         return .getMonthOfYear(instant);
     }
 
     //-----------------------------------------------------------------------
     
Add the specified month to the specified time instant. The amount added may be negative.

If the new month has less total days than the specified day of the month, this value is coerced to the nearest sane value. e.g.

07-31 - (1 month) = 06-30

03-31 - (1 month) = 02-28 or 02-29 depending

Parameters:
instant the time instant in millis to update.
months the months to add (can be negative).
Returns:
the updated time instant.
See also:
org.joda.time.DateTimeField.add(long,int)
org.joda.time.ReadWritableDateTime.addMonths(int)
 
     public long add(long instantint months) {
         if (months == 0) {
             return instant// the easy case
         }
         //
         // Save time part first.
         //
         long timePart = .getMillisOfDay(instant);
        //
        //
        // Get this year and month.
        //
        int thisYear = .getYear(instant);
        int thisMonth = .getMonthOfYear(instantthisYear);
        // ----------------------------------------------------------
        //
        // Do not refactor without careful consideration.
        // Order of calculation is important.
        //
        int yearToUse;
        // Initially, monthToUse is zero-based
        int monthToUse = thisMonth - 1 + months;
        if (monthToUse >= 0) {
            yearToUse = thisYear + (monthToUse / );
            monthToUse = (monthToUse % ) + 1;
        } else {
            yearToUse = thisYear + (monthToUse / ) - 1;
            monthToUse = Math.abs(monthToUse);
            int remMonthToUse = monthToUse % ;
            // Take care of the boundary condition
            if (remMonthToUse == 0) {
                remMonthToUse = ;
            }
            monthToUse =  - remMonthToUse + 1;
            // Take care of the boundary condition
            if (monthToUse == 1) {
                yearToUse += 1;
            }
        }
        // End of do not refactor.
        // ----------------------------------------------------------
        //
        // Quietly force DOM to nearest sane value.
        //
        int dayToUse = .getDayOfMonth(instantthisYearthisMonth);
        int maxDay = .getDaysInYearMonth(yearToUsemonthToUse);
        if (dayToUse > maxDay) {
            dayToUse = maxDay;
        }
        //
        // get proper date part, and return result
        //
        long datePart =
            .getYearMonthDayMillis(yearToUsemonthToUsedayToUse);
        return datePart + timePart;
    }
    //-----------------------------------------------------------------------
    public long add(long instantlong months) {
        int i_months = (int)months;
        if (i_months == months) {
            return add(instanti_months);
        }
        // Copied from add(long, int) and modified slightly:
        long timePart = .getMillisOfDay(instant);
        int thisYear = .getYear(instant);
        int thisMonth = .getMonthOfYear(instantthisYear);
        long yearToUse;
        long monthToUse = thisMonth - 1 + months;
        if (monthToUse >= 0) {
            yearToUse = thisYear + (monthToUse / );
            monthToUse = (monthToUse % ) + 1;
        } else {
            yearToUse = thisYear + (monthToUse / ) - 1;
            monthToUse = Math.abs(monthToUse);
            int remMonthToUse = (int)(monthToUse % );
            if (remMonthToUse == 0) {
                remMonthToUse = ;
            }
            monthToUse =  - remMonthToUse + 1;
            if (monthToUse == 1) {
                yearToUse += 1;
            }
        }
        if (yearToUse < .getMinYear() ||
            yearToUse > .getMaxYear()) {
            throw new IllegalArgumentException
                ("Magnitude of add amount is too large: " + months);
        }
        int i_yearToUse = (int)yearToUse;
        int i_monthToUse = (int)monthToUse;
        int dayToUse = .getDayOfMonth(instantthisYearthisMonth);
        int maxDay = .getDaysInYearMonth(i_yearToUsei_monthToUse);
        if (dayToUse > maxDay) {
            dayToUse = maxDay;
        }
        long datePart =
            .getYearMonthDayMillis(i_yearToUsei_monthToUsedayToUse);
        return datePart + timePart;
    }
    //-----------------------------------------------------------------------
    public int[] add(ReadablePartial partialint fieldIndexint[] valuesint valueToAdd) {
        // overridden as superclass algorithm can't handle
        // 2004-02-29 + 48 months -> 2008-02-29 type dates
        if (valueToAdd == 0) {
            return values;
        }
        if (partial.size() > 0 && partial.getFieldType(0).equals(DateTimeFieldType.monthOfYear()) && fieldIndex == 0) {
            // month is largest field and being added to, such as month-day
            int curMonth0 = partial.getValue(0) - 1;
            int newMonth = ((curMonth0 + (valueToAdd % 12) + 12) % 12) + 1;
            return set(partial, 0, valuesnewMonth);
        }
        if (DateTimeUtils.isContiguous(partial)) {
            long instant = 0L;
            for (int i = 0, isize = partial.size(); i < isizei++) {
                instant = partial.getFieldType(i).getField().set(instantvalues[i]);
            }
            instant = add(instantvalueToAdd);
            return .get(partialinstant);
        } else {
            return super.add(partialfieldIndexvaluesvalueToAdd);
        }
    }
    //-----------------------------------------------------------------------
    
Add to the Month component of the specified time instant wrapping around within that component if necessary.

Parameters:
instant the time instant in millis to update.
months the months to add (can be negative).
Returns:
the updated time instant.
See also:
org.joda.time.DateTimeField.addWrapField(long,int)
    public long addWrapField(long instantint months) {
        return set(instant, FieldUtils.getWrappedValue(get(instant), months));
    }
    //-----------------------------------------------------------------------
    public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
        if (minuendInstant < subtrahendInstant) {
            return -getDifference(subtrahendInstantminuendInstant);
        }
        int minuendYear = .getYear(minuendInstant);
        int minuendMonth = .getMonthOfYear(minuendInstantminuendYear);
        int subtrahendYear = .getYear(subtrahendInstant);
        int subtrahendMonth = .getMonthOfYear(subtrahendInstantsubtrahendYear);
        long difference = (minuendYear - subtrahendYear) * ((long) + minuendMonth - subtrahendMonth;
        // Before adjusting for remainder, account for special case of add
        // where the day-of-month is forced to the nearest sane value.
        int minuendDom = .getDayOfMonth
            (minuendInstantminuendYearminuendMonth);
        if (minuendDom == .getDaysInYearMonth(minuendYearminuendMonth)) {
            // Last day of the minuend month...
            int subtrahendDom = .getDayOfMonth
                (subtrahendInstantsubtrahendYearsubtrahendMonth);
            if (subtrahendDom > minuendDom) {
                // ...and day of subtrahend month is larger.
                // Note: This works fine, but it ideally shouldn't invoke other
                // fields from within a field.
                subtrahendInstant = .dayOfMonth().set(subtrahendInstantminuendDom);
            }
        }
        // Inlined remainder method to avoid duplicate calls.
        long minuendRem = minuendInstant
            - .getYearMonthMillis(minuendYearminuendMonth);
        long subtrahendRem = subtrahendInstant
            - .getYearMonthMillis(subtrahendYearsubtrahendMonth);
        if (minuendRem < subtrahendRem) {
            difference--;
        }
        return difference;
    }
    //-----------------------------------------------------------------------
    
Set the Month component of the specified time instant.

If the new month has less total days than the specified day of the month, this value is coerced to the nearest sane value. e.g.

07-31 to month 6 = 06-30

03-31 to month 2 = 02-28 or 02-29 depending

Parameters:
instant the time instant in millis to update.
month the month (1,12) to update the time to.
Returns:
the updated time instant.
Throws:
java.lang.IllegalArgumentException if month is invalid
    public long set(long instantint month) {
        FieldUtils.verifyValueBounds(thismonth);
        //
        int thisYear = .getYear(instant);
        //
        int thisDom = .getDayOfMonth(instantthisYear);
        int maxDom = .getDaysInYearMonth(thisYearmonth);
        if (thisDom > maxDom) {
            // Quietly force DOM to nearest sane value.
            thisDom = maxDom;
        }
        // Return newly calculated millis value
        return .getYearMonthDayMillis(thisYearmonththisDom) +
            .getMillisOfDay(instant);
    }
    //-----------------------------------------------------------------------
        return .years();
    }
    //-----------------------------------------------------------------------
    public boolean isLeap(long instant) {
        int thisYear = .getYear(instant);
        if (.isLeapYear(thisYear)) {
            return (.getMonthOfYear(instantthisYear) == );
        }
        return false;
    }
    //-----------------------------------------------------------------------
    public int getLeapAmount(long instant) {
        return isLeap(instant) ? 1 : 0;
    }
    //-----------------------------------------------------------------------
        return .days();
    }
    //-----------------------------------------------------------------------
    public int getMinimumValue() {
        return ;
    }
    //-----------------------------------------------------------------------
    public int getMaximumValue() {
        return ;
    }
    //-----------------------------------------------------------------------
    public long roundFloor(long instant) {
        int year = .getYear(instant);
        int month = .getMonthOfYear(instantyear);
        return .getYearMonthMillis(yearmonth);
    }
    //-----------------------------------------------------------------------
    public long remainder(long instant) {
        return instant - roundFloor(instant);
    }
    //-----------------------------------------------------------------------
    
Serialization singleton
    private Object readResolve() {
        return .monthOfYear();
    }
New to GrepCode? Check out our FAQ X