2
* Copyright 2001-2006 Stephen Colebourne
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.joda.time;
18
import org.joda.time.base.BaseSingleFieldPeriod;
19
import org.joda.time.field.FieldUtils;
20
import org.joda.time.format.ISOPeriodFormat;
21
import org.joda.time.format.PeriodFormatter;
24
* An immutable time period representing a number of weeks.
26
* <code>Weeks</code> is an immutable period that can only store weeks.
27
* It does not store years, months or hours for example. As such it is a
28
* type-safe way of representing a number of weeks in an application.
30
* The number of weeks is set in the constructor, and may be queried using
31
* <code>getWeeks()</code>. Basic mathematical operations are provided -
32
* <code>plus()</code>, <code>minus()</code>, <code>multipliedBy()</code> and
33
* <code>dividedBy()</code>.
35
* <code>Weeks</code> is thread-safe and immutable.
37
* @author Stephen Colebourne
40
public final class Weeks extends BaseSingleFieldPeriod {
42
/** Constant representing zero weeks. */
43
public static final Weeks ZERO = new Weeks(0);
44
/** Constant representing one week. */
45
public static final Weeks ONE = new Weeks(1);
46
/** Constant representing two weeks. */
47
public static final Weeks TWO = new Weeks(2);
48
/** Constant representing three weeks. */
49
public static final Weeks THREE = new Weeks(3);
50
/** Constant representing the maximum number of weeks that can be stored in this object. */
51
public static final Weeks MAX_VALUE = new Weeks(Integer.MAX_VALUE);
52
/** Constant representing the minimum number of weeks that can be stored in this object. */
53
public static final Weeks MIN_VALUE = new Weeks(Integer.MIN_VALUE);
55
/** The paser to use for this class. */
56
private static final PeriodFormatter PARSER = ISOPeriodFormat.standard().withParseType(PeriodType.weeks());
57
/** Serialization version. */
58
private static final long serialVersionUID = 87525275727380866L;
60
//-----------------------------------------------------------------------
62
* Obtains an instance of <code>Weeks</code> that may be cached.
63
* <code>Weeks</code> is immutable, so instances can be cached and shared.
64
* This factory method provides access to shared instances.
66
* @param weeks the number of weeks to obtain an instance for
67
* @return the instance of Weeks
69
public static Weeks weeks(int weeks) {
79
case Integer.MAX_VALUE:
81
case Integer.MIN_VALUE:
84
return new Weeks(weeks);
88
//-----------------------------------------------------------------------
90
* Creates a <code>Weeks</code> representing the number of whole weeks
91
* between the two specified datetimes.
93
* @param start the start instant, must not be null
94
* @param end the end instant, must not be null
95
* @return the period in weeks
96
* @throws IllegalArgumentException if the instants are null or invalid
98
public static Weeks weeksBetween(ReadableInstant start, ReadableInstant end) {
99
int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.weeks());
100
return Weeks.weeks(amount);
104
* Creates a <code>Weeks</code> representing the number of whole weeks
105
* between the two specified partial datetimes.
107
* The two partials must contain the same fields, for example you can specify
108
* two <code>LocalDate</code> objects.
110
* @param start the start partial date, must not be null
111
* @param end the end partial date, must not be null
112
* @return the period in weeks
113
* @throws IllegalArgumentException if the partials are null or invalid
115
public static Weeks weeksBetween(ReadablePartial start, ReadablePartial end) {
116
if (start instanceof LocalDate && end instanceof LocalDate) {
117
Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
118
int weeks = chrono.weeks().getDifference(
119
((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis());
120
return Weeks.weeks(weeks);
122
int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
123
return Weeks.weeks(amount);
127
* Creates a <code>Weeks</code> representing the number of whole weeks
128
* in the specified interval.
130
* @param interval the interval to extract weeks from, null returns zero
131
* @return the period in weeks
132
* @throws IllegalArgumentException if the partials are null or invalid
134
public static Weeks weeksIn(ReadableInterval interval) {
135
if (interval == null) {
138
int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.weeks());
139
return Weeks.weeks(amount);
143
* Creates a new <code>Weeks</code> representing the number of complete
144
* standard length weeks in the specified period.
146
* This factory method converts all fields from the period to hours using standardised
147
* durations for each field. Only those fields which have a precise duration in
148
* the ISO UTC chronology can be converted.
150
* <li>One week consists of 7 days.
151
* <li>One day consists of 24 hours.
152
* <li>One hour consists of 60 minutes.
153
* <li>One minute consists of 60 weeks.
154
* <li>One second consists of 1000 milliseconds.
156
* Months and Years are imprecise and periods containing these values cannot be converted.
158
* @param period the period to get the number of hours from, null returns zero
159
* @return the period in weeks
160
* @throws IllegalArgumentException if the period contains imprecise duration values
162
public static Weeks standardWeeksIn(ReadablePeriod period) {
163
int amount = BaseSingleFieldPeriod.standardPeriodIn(period, DateTimeConstants.MILLIS_PER_WEEK);
164
return Weeks.weeks(amount);
168
* Creates a new <code>Weeks</code> by parsing a string in the ISO8601 format 'PnW'.
170
* The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
171
* weeks component may be non-zero. If any other component is non-zero, an exception
174
* @param periodStr the period string, null returns zero
175
* @return the period in weeks
176
* @throws IllegalArgumentException if the string format is invalid
178
public static Weeks parseWeeks(String periodStr) {
179
if (periodStr == null) {
182
Period p = PARSER.parsePeriod(periodStr);
183
return Weeks.weeks(p.getWeeks());
186
//-----------------------------------------------------------------------
188
* Creates a new instance representing a number of weeks.
189
* You should consider using the factory method {@link #weeks(int)}
190
* instead of the constructor.
192
* @param weeks the number of weeks to represent
194
private Weeks(int weeks) {
199
* Resolves singletons.
201
* @return the singleton instance
203
private Object readResolve() {
204
return Weeks.weeks(getValue());
207
//-----------------------------------------------------------------------
209
* Gets the duration field type, which is <code>weeks</code>.
211
* @return the period type
213
public DurationFieldType getFieldType() {
214
return DurationFieldType.weeks();
218
* Gets the period type, which is <code>weeks</code>.
220
* @return the period type
222
public PeriodType getPeriodType() {
223
return PeriodType.weeks();
226
//-----------------------------------------------------------------------
228
* Converts this period in weeks to a period in days assuming a
231
* This method allows you to convert between different types of period.
232
* However to achieve this it makes the assumption that all weeks are
234
* This may not be true for some unusual chronologies. However, it is included
235
* as it is a useful operation for many applications and business rules.
237
* @return a period representing the number of days for this number of weeks
238
* @throws ArithmeticException if the number of days is too large to be represented
240
public Days toStandardDays() {
241
return Days.days(FieldUtils.safeMultiply(getValue(), DateTimeConstants.DAYS_PER_WEEK));
245
* Converts this period in weeks to a period in hours assuming a
246
* 7 day week and 24 hour day.
248
* This method allows you to convert between different types of period.
249
* However to achieve this it makes the assumption that all weeks are
250
* 7 days long and all days are 24 hours long.
251
* This is not true when daylight savings is considered and may also not
252
* be true for some unusual chronologies. However, it is included
253
* as it is a useful operation for many applications and business rules.
255
* @return a period representing the number of hours for this number of weeks
256
* @throws ArithmeticException if the number of hours is too large to be represented
258
public Hours toStandardHours() {
259
return Hours.hours(FieldUtils.safeMultiply(getValue(), DateTimeConstants.HOURS_PER_WEEK));
263
* Converts this period in weeks to a period in minutes assuming a
264
* 7 day week, 24 hour day and 60 minute hour.
266
* This method allows you to convert between different types of period.
267
* However to achieve this it makes the assumption that all weeks are
268
* 7 days long, all days are 24 hours long and all hours are 60 minutes long.
269
* This is not true when daylight savings is considered and may also not
270
* be true for some unusual chronologies. However, it is included
271
* as it is a useful operation for many applications and business rules.
273
* @return a period representing the number of minutes for this number of weeks
274
* @throws ArithmeticException if the number of minutes is too large to be represented
276
public Minutes toStandardMinutes() {
277
return Minutes.minutes(FieldUtils.safeMultiply(getValue(), DateTimeConstants.MINUTES_PER_WEEK));
281
* Converts this period in weeks to a period in seconds assuming a
282
* 7 day week, 24 hour day, 60 minute hour and 60 second minute.
284
* This method allows you to convert between different types of period.
285
* However to achieve this it makes the assumption that all weeks are
286
* 7 days long, all days are 24 hours long, all hours are 60 minutes long
287
* and all minutes are 60 seconds long.
288
* This is not true when daylight savings is considered and may also not
289
* be true for some unusual chronologies. However, it is included
290
* as it is a useful operation for many applications and business rules.
292
* @return a period representing the number of seconds for this number of weeks
293
* @throws ArithmeticException if the number of seconds is too large to be represented
295
public Seconds toStandardSeconds() {
296
return Seconds.seconds(FieldUtils.safeMultiply(getValue(), DateTimeConstants.SECONDS_PER_WEEK));
299
//-----------------------------------------------------------------------
301
* Converts this period in weeks to a duration in milliweeks assuming a
302
* 7 day week, 24 hour day, 60 minute hour and 60 second minute.
304
* This method allows you to convert from a period to a duration.
305
* However to achieve this it makes the assumption that all weeks are
306
* 7 days long, all days are 24 hours long, all hours are 60 minutes long
307
* and all minutes are 60 seconds long.
308
* This is not true when daylight savings time is considered, and may also
309
* not be true for some unusual chronologies. However, it is included as it
310
* is a useful operation for many applications and business rules.
312
* @return a duration equivalent to this number of weeks
314
public Duration toStandardDuration() {
315
long weeks = getValue(); // assign to a long
316
return new Duration(weeks * DateTimeConstants.MILLIS_PER_WEEK);
319
//-----------------------------------------------------------------------
321
* Gets the number of weeks that this period represents.
323
* @return the number of weeks in the period
325
public int getWeeks() {
329
//-----------------------------------------------------------------------
331
* Returns a new instance with the specified number of weeks added.
333
* This instance is immutable and unaffected by this method call.
335
* @param weeks the amount of weeks to add, may be negative
336
* @return the new period plus the specified number of weeks
337
* @throws ArithmeticException if the result overflows an int
339
public Weeks plus(int weeks) {
343
return Weeks.weeks(FieldUtils.safeAdd(getValue(), weeks));
347
* Returns a new instance with the specified number of weeks added.
349
* This instance is immutable and unaffected by this method call.
351
* @param weeks the amount of weeks to add, may be negative, null means zero
352
* @return the new period plus the specified number of weeks
353
* @throws ArithmeticException if the result overflows an int
355
public Weeks plus(Weeks weeks) {
359
return plus(weeks.getValue());
362
//-----------------------------------------------------------------------
364
* Returns a new instance with the specified number of weeks taken away.
366
* This instance is immutable and unaffected by this method call.
368
* @param weeks the amount of weeks to take away, may be negative
369
* @return the new period minus the specified number of weeks
370
* @throws ArithmeticException if the result overflows an int
372
public Weeks minus(int weeks) {
373
return plus(FieldUtils.safeNegate(weeks));
377
* Returns a new instance with the specified number of weeks taken away.
379
* This instance is immutable and unaffected by this method call.
381
* @param weeks the amount of weeks to take away, may be negative, null means zero
382
* @return the new period minus the specified number of weeks
383
* @throws ArithmeticException if the result overflows an int
385
public Weeks minus(Weeks weeks) {
389
return minus(weeks.getValue());
392
//-----------------------------------------------------------------------
394
* Returns a new instance with the weeks multiplied by the specified scalar.
396
* This instance is immutable and unaffected by this method call.
398
* @param scalar the amount to multiply by, may be negative
399
* @return the new period multiplied by the specified scalar
400
* @throws ArithmeticException if the result overflows an int
402
public Weeks multipliedBy(int scalar) {
403
return Weeks.weeks(FieldUtils.safeMultiply(getValue(), scalar));
407
* Returns a new instance with the weeks divided by the specified divisor.
408
* The calculation uses integer division, thus 3 divided by 2 is 1.
410
* This instance is immutable and unaffected by this method call.
412
* @param divisor the amount to divide by, may be negative
413
* @return the new period divided by the specified divisor
414
* @throws ArithmeticException if the divisor is zero
416
public Weeks dividedBy(int divisor) {
420
return Weeks.weeks(getValue() / divisor);
423
//-----------------------------------------------------------------------
425
* Returns a new instance with the weeks value negated.
427
* @return the new period with a negated value
428
* @throws ArithmeticException if the result overflows an int
430
public Weeks negated() {
431
return Weeks.weeks(FieldUtils.safeNegate(getValue()));
434
//-----------------------------------------------------------------------
436
* Is this weeks instance greater than the specified number of weeks.
438
* @param other the other period, null means zero
439
* @return true if this weeks instance is greater than the specified one
441
public boolean isGreaterThan(Weeks other) {
443
return getValue() > 0;
445
return getValue() > other.getValue();
449
* Is this weeks instance less than the specified number of weeks.
451
* @param other the other period, null means zero
452
* @return true if this weeks instance is less than the specified one
454
public boolean isLessThan(Weeks other) {
456
return getValue() < 0;
458
return getValue() < other.getValue();
461
//-----------------------------------------------------------------------
463
* Gets this instance as a String in the ISO8601 duration format.
465
* For example, "P4W" represents 4 weeks.
467
* @return the value as an ISO8601 string
469
public String toString() {
470
return "P" + String.valueOf(getValue()) + "W";