< prev index next >

src/java.base/share/classes/java/time/temporal/WeekFields.java

Print this page




 225      * The unit that represents week-based-years for the purpose of addition and subtraction.
 226      * <p>
 227      * This allows a number of week-based-years to be added to, or subtracted from, a date.
 228      * The unit is equal to either 52 or 53 weeks.
 229      * The estimated duration of a week-based-year is the same as that of a standard ISO
 230      * year at {@code 365.2425 Days}.
 231      * <p>
 232      * The rules for addition add the number of week-based-years to the existing value
 233      * for the week-based-year field retaining the week-of-week-based-year
 234      * and day-of-week, unless the week number it too large for the target year.
 235      * In that case, the week is set to the last week of the year
 236      * with the same day-of-week.
 237      * <p>
 238      * This unit is an immutable and thread-safe singleton.
 239      */
 240     public static final TemporalUnit WEEK_BASED_YEARS = IsoFields.WEEK_BASED_YEARS;
 241 
 242     /**
 243      * Serialization version.
 244      */

 245     private static final long serialVersionUID = -1177360819670808121L;
 246 
 247     /**
 248      * The first day-of-week.
 249      */
 250     private final DayOfWeek firstDayOfWeek;
 251     /**
 252      * The minimal number of days in the first week.
 253      */
 254     private final int minimalDays;
 255     /**
 256      * The field used to access the computed DayOfWeek.
 257      */
 258     private final transient TemporalField dayOfWeek = ComputedDayOfField.ofDayOfWeekField(this);
 259     /**
 260      * The field used to access the computed WeekOfMonth.
 261      */
 262     private final transient TemporalField weekOfMonth = ComputedDayOfField.ofWeekOfMonthField(this);
 263     /**
 264      * The field used to access the computed WeekOfYear.


 345      */
 346     private WeekFields(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
 347         Objects.requireNonNull(firstDayOfWeek, "firstDayOfWeek");
 348         if (minimalDaysInFirstWeek < 1 || minimalDaysInFirstWeek > 7) {
 349             throw new IllegalArgumentException("Minimal number of days is invalid");
 350         }
 351         this.firstDayOfWeek = firstDayOfWeek;
 352         this.minimalDays = minimalDaysInFirstWeek;
 353     }
 354 
 355     //-----------------------------------------------------------------------
 356     /**
 357      * Restore the state of a WeekFields from the stream.
 358      * Check that the values are valid.
 359      *
 360      * @param s the stream to read
 361      * @throws InvalidObjectException if the serialized object has an invalid
 362      *     value for firstDayOfWeek or minimalDays.
 363      * @throws ClassNotFoundException if a class cannot be resolved
 364      */

 365     private void readObject(ObjectInputStream s)
 366          throws IOException, ClassNotFoundException, InvalidObjectException
 367     {
 368         s.defaultReadObject();
 369         if (firstDayOfWeek == null) {
 370             throw new InvalidObjectException("firstDayOfWeek is null");
 371         }
 372 
 373         if (minimalDays < 1 || minimalDays > 7) {
 374             throw new InvalidObjectException("Minimal number of days is invalid");
 375         }
 376     }
 377 
 378     /**
 379      * Return the singleton WeekFields associated with the
 380      * {@code firstDayOfWeek} and {@code minimalDays}.
 381      * @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
 382      * @throws InvalidObjectException if the serialized object has invalid
 383      *     values for firstDayOfWeek or minimalDays.
 384      */

 385     private Object readResolve() throws InvalidObjectException {
 386         try {
 387             return WeekFields.of(firstDayOfWeek, minimalDays);
 388         } catch (IllegalArgumentException iae) {
 389             throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
 390         }
 391     }
 392 
 393     //-----------------------------------------------------------------------
 394     /**
 395      * Gets the first day-of-week.
 396      * <p>
 397      * The first day-of-week varies by culture.
 398      * For example, the US uses Sunday, while France and the ISO-8601 standard use Monday.
 399      * This method returns the first day using the standard {@code DayOfWeek} enum.
 400      *
 401      * @return the first day-of-week, not null
 402      */
 403     public DayOfWeek getFirstDayOfWeek() {
 404         return firstDayOfWeek;




 225      * The unit that represents week-based-years for the purpose of addition and subtraction.
 226      * <p>
 227      * This allows a number of week-based-years to be added to, or subtracted from, a date.
 228      * The unit is equal to either 52 or 53 weeks.
 229      * The estimated duration of a week-based-year is the same as that of a standard ISO
 230      * year at {@code 365.2425 Days}.
 231      * <p>
 232      * The rules for addition add the number of week-based-years to the existing value
 233      * for the week-based-year field retaining the week-of-week-based-year
 234      * and day-of-week, unless the week number it too large for the target year.
 235      * In that case, the week is set to the last week of the year
 236      * with the same day-of-week.
 237      * <p>
 238      * This unit is an immutable and thread-safe singleton.
 239      */
 240     public static final TemporalUnit WEEK_BASED_YEARS = IsoFields.WEEK_BASED_YEARS;
 241 
 242     /**
 243      * Serialization version.
 244      */
 245     @java.io.Serial
 246     private static final long serialVersionUID = -1177360819670808121L;
 247 
 248     /**
 249      * The first day-of-week.
 250      */
 251     private final DayOfWeek firstDayOfWeek;
 252     /**
 253      * The minimal number of days in the first week.
 254      */
 255     private final int minimalDays;
 256     /**
 257      * The field used to access the computed DayOfWeek.
 258      */
 259     private final transient TemporalField dayOfWeek = ComputedDayOfField.ofDayOfWeekField(this);
 260     /**
 261      * The field used to access the computed WeekOfMonth.
 262      */
 263     private final transient TemporalField weekOfMonth = ComputedDayOfField.ofWeekOfMonthField(this);
 264     /**
 265      * The field used to access the computed WeekOfYear.


 346      */
 347     private WeekFields(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
 348         Objects.requireNonNull(firstDayOfWeek, "firstDayOfWeek");
 349         if (minimalDaysInFirstWeek < 1 || minimalDaysInFirstWeek > 7) {
 350             throw new IllegalArgumentException("Minimal number of days is invalid");
 351         }
 352         this.firstDayOfWeek = firstDayOfWeek;
 353         this.minimalDays = minimalDaysInFirstWeek;
 354     }
 355 
 356     //-----------------------------------------------------------------------
 357     /**
 358      * Restore the state of a WeekFields from the stream.
 359      * Check that the values are valid.
 360      *
 361      * @param s the stream to read
 362      * @throws InvalidObjectException if the serialized object has an invalid
 363      *     value for firstDayOfWeek or minimalDays.
 364      * @throws ClassNotFoundException if a class cannot be resolved
 365      */
 366     @java.io.Serial
 367     private void readObject(ObjectInputStream s)
 368          throws IOException, ClassNotFoundException, InvalidObjectException
 369     {
 370         s.defaultReadObject();
 371         if (firstDayOfWeek == null) {
 372             throw new InvalidObjectException("firstDayOfWeek is null");
 373         }
 374 
 375         if (minimalDays < 1 || minimalDays > 7) {
 376             throw new InvalidObjectException("Minimal number of days is invalid");
 377         }
 378     }
 379 
 380     /**
 381      * Return the singleton WeekFields associated with the
 382      * {@code firstDayOfWeek} and {@code minimalDays}.
 383      * @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
 384      * @throws InvalidObjectException if the serialized object has invalid
 385      *     values for firstDayOfWeek or minimalDays.
 386      */
 387     @java.io.Serial
 388     private Object readResolve() throws InvalidObjectException {
 389         try {
 390             return WeekFields.of(firstDayOfWeek, minimalDays);
 391         } catch (IllegalArgumentException iae) {
 392             throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
 393         }
 394     }
 395 
 396     //-----------------------------------------------------------------------
 397     /**
 398      * Gets the first day-of-week.
 399      * <p>
 400      * The first day-of-week varies by culture.
 401      * For example, the US uses Sunday, while France and the ISO-8601 standard use Monday.
 402      * This method returns the first day using the standard {@code DayOfWeek} enum.
 403      *
 404      * @return the first day-of-week, not null
 405      */
 406     public DayOfWeek getFirstDayOfWeek() {
 407         return firstDayOfWeek;


< prev index next >