Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 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 week of the weekyear component of time.

Author(s):
Guy Allard
Stephen Colebourne
Brian S O'Neill
Since:
1.1, refactored from GJWeekyearDateTimeField
 
     
     private static final long serialVersionUID = 6215066916806820644L;
 
     private static final long WEEK_53 = (53L - 1) * .;
 
     private final BasicChronology iChronology;

    
Restricted constructor
 
         super(DateTimeFieldType.weekyear(), chronology.getAverageMillisPerYear());
          = chronology;
     }
 
     public boolean isLenient() {
         return false;
     }

    
Get the Year of a week based year component of the specified time instant.

Parameters:
instant the time instant in millis to query.
Returns:
the year extracted from the input.
See also:
org.joda.time.DateTimeField.get(long)
 
     public int get(long instant) {
         return .getWeekyear(instant);
     }

    
Add the specified years to the specified time instant.

Parameters:
instant the time instant in millis to update.
years the years to add (can be negative).
Returns:
the updated time instant.
See also:
org.joda.time.DateTimeField.add(long,int)
 
     public long add(long instantint years) {
         if (years == 0) {
             return instant;
         }
         return set(instantget(instant) + years);
     }
 
     public long add(long instantlong value) {
         return add(instant, FieldUtils.safeToInt(value));
     }

    
Add to the year component of the specified time instant wrapping around within that component if necessary.

Parameters:
instant the time instant in millis to update.
years the years to add (can be negative).
Returns:
the updated time instant.
See also:
org.joda.time.DateTimeField.addWrapField(long,int)
 
     public long addWrapField(long instantint years) {
         return add(instantyears);
     }
 
     public long getDifferenceAsLong(long minuendInstantlong subtrahendInstant) {
         if (minuendInstant < subtrahendInstant) {
             return -getDifference(subtrahendInstantminuendInstant);
         }
 
        int minuendWeekyear = get(minuendInstant);
        int subtrahendWeekyear = get(subtrahendInstant);
        long minuendRem = remainder(minuendInstant);
        long subtrahendRem = remainder(subtrahendInstant);
        // Balance leap weekyear differences on remainders.
        if (subtrahendRem >=  && .getWeeksInYear(minuendWeekyear) <= 52) {
            subtrahendRem -= .;
        }
        int difference = minuendWeekyear - subtrahendWeekyear;
        if (minuendRem < subtrahendRem) {
            difference--;
        }
        return difference;
    }

    
Set the Year of a week based year component of the specified time instant.

Parameters:
instant the time instant in millis to update.
year the year (-9999,9999) to set the date to.
Returns:
the updated DateTime.
Throws:
java.lang.IllegalArgumentException if year is invalid.
See also:
org.joda.time.DateTimeField.set(long,int)
    public long set(long instantint year) {
        FieldUtils.verifyValueBounds(this, Math.abs(year),
                                     .getMinYear(), .getMaxYear());
        //
        // Do nothing if no real change is requested.
        //
        int thisWeekyear = getinstant );
        if ( thisWeekyear == year ) {
            return instant;
        }
        //
        // Calculate the DayOfWeek (to be preserved).
        //
        int thisDow = .getDayOfWeek(instant);
        //
        // Calculate the maximum weeks in the target year.
        //
        int weeksInFromYear = .getWeeksInYearthisWeekyear );
        int weeksInToYear = .getWeeksInYearyear );
        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 = .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 = .setYearworkInstantyear );
        //
        // Calculate the weekyear number for the get close to value
        // (which might not be equal to the year just set).
        //
        int workWoyYear = getworkInstant );
        //
        // At most we are off by one year, which can be "fixed" by
        // adding/subtracting a week.
        //
        if ( workWoyYear < year ) {
            workInstant += .;
        } else if ( workWoyYear > year ) {
            workInstant -= .;
        }
        //
        // Set the proper week in the current weekyear.
        //
        // BEGIN: possible set WeekOfWeekyear logic.
        int currentWoyWeek = .getWeekOfWeekyear(workInstant);
        // No range check required (we already know it is OK).
        workInstant = workInstant + (setToWeek - currentWoyWeek)
            * (long).;
        // 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 = .dayOfWeek().setworkInstantthisDow );
        //
        // Return result.
        //
        return workInstant;
    }
        return null;
    }
    public boolean isLeap(long instant) {
        return .getWeeksInYear(.getWeekyear(instant)) > 52;
    }
    public int getLeapAmount(long instant) {
        return .getWeeksInYear(.getWeekyear(instant)) - 52;
    }
        return .weeks();
    }
    public int getMinimumValue() {
        return .getMinYear();
    }
    public int getMaximumValue() {
        return .getMaxYear();
    }
    public long roundFloor(long instant) {
        // Note: This works fine, but it ideally shouldn't invoke other
        // fields from within a field.
        instant = .weekOfWeekyear().roundFloor(instant);
        int wow = .getWeekOfWeekyear(instant);
        if (wow > 1) {
            instant -= ((long.) * (wow - 1);
        }
        return instant;
    }
    public long remainder(long instant) {
        return instant - roundFloor(instant);
    }

    
Serialization singleton
    private Object readResolve() {
        return .weekyear();
    }
New to GrepCode? Check out our FAQ X