-
Notifications
You must be signed in to change notification settings - Fork 668
/
TemporalAdjusters.java
626 lines (582 loc) · 27.3 KB
/
TemporalAdjusters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2012-2013, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.temporal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Objects;
import java.util.function.UnaryOperator;
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.YEARS;
/**
* Common and useful TemporalAdjusters.
* <p>
* Adjusters are a key tool for modifying temporal objects.
* They exist to externalize the process of adjustment, permitting different
* approaches, as per the strategy design pattern.
* Examples might be an adjuster that sets the date avoiding weekends, or one that
* sets the date to the last day of the month.
* <p>
* There are two equivalent ways of using a {@code TemporalAdjuster}.
* The first is to invoke the method on the interface directly.
* The second is to use {@link Temporal#with(TemporalAdjuster)}:
* <pre>
* // these two lines are equivalent, but the second approach is recommended
* temporal = thisAdjuster.adjustInto(temporal);
* temporal = temporal.with(thisAdjuster);
* </pre>
* It is recommended to use the second approach, {@code with(TemporalAdjuster)},
* as it is a lot clearer to read in code.
* <p>
* This class contains a standard set of adjusters, available as static methods.
* These include:
* <ul>
* <li>finding the first or last day of the month
* <li>finding the first day of next month
* <li>finding the first or last day of the year
* <li>finding the first day of next year
* <li>finding the first or last day-of-week within a month, such as "first Wednesday in June"
* <li>finding the next or previous day-of-week, such as "next Thursday"
* </ul>
*
* @implSpec All the implementations supplied by the static methods are immutable.
* @see TemporalAdjuster
* @since 1.8
*/
// 时间量整合器工厂,内部预设了多种实用的整合器
public final class TemporalAdjusters {
/**
* Private constructor since this is a utility class.
*/
private TemporalAdjusters() {
}
/**
* Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
* <p>
* The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
* This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
* to be wrapped to match the temporal-based interface.
* This is provided for convenience to make user-written adjusters simpler.
* <p>
* In general, user-written adjusters should be static constants:
* <pre>{@code
* static TemporalAdjuster TWO_DAYS_LATER =
* TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2));
* }</pre>
*
* @param dateBasedAdjuster the date-based adjuster, not null
*
* @return the temporal adjuster wrapping on the date adjuster, not null
*/
// 返回一个时间量整合器,该整合期支持使用dateBasedAdjuster处理目标时间量中的LocalDate信息
public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
Objects.requireNonNull(dateBasedAdjuster, "dateBasedAdjuster");
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
/*
* 从temporal中查询LocalDate部件。
*
* 如果没有现成的部件,通常需要从temporal中解析出纪元天,
* 然后使用纪元天构造LocalDate后返回。
*/
LocalDate input = LocalDate.from(temporal);
// 使用dateBasedAdjuster处理input后返回处理结果
LocalDate output = dateBasedAdjuster.apply(input);
// 将output与temporal中其他类型的字段整合后返回
return temporal.with(output);
}
};
}
/**
* Returns the "first day of month" adjuster, which returns a new date set to
* the first day of the current month.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 will return 2011-01-01.<br>
* The input 2011-02-15 will return 2011-02-01.
* <p>
* The behavior is suitable for use with most calendar systems.
* It is equivalent to:
* <pre>
* temporal.with(DAY_OF_MONTH, 1);
* </pre>
*
* @return the first day-of-month adjuster, not null
*/
// 返回一个时间量整合器,该整合器通常是用temporal所在月的第一天这个时间点来更新temporal的"天"部件
public static TemporalAdjuster firstDayOfMonth() {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
return temporal.with(ChronoField.DAY_OF_MONTH, 1);
}
};
}
/**
* Returns the "last day of month" adjuster, which returns a new date set to
* the last day of the current month.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 will return 2011-01-31.<br>
* The input 2011-02-15 will return 2011-02-28.<br>
* The input 2012-02-15 will return 2012-02-29 (leap year).<br>
* The input 2011-04-15 will return 2011-04-30.
* <p>
* The behavior is suitable for use with most calendar systems.
* It is equivalent to:
* <pre>
* long lastDay = temporal.range(DAY_OF_MONTH).getMaximum();
* temporal.with(DAY_OF_MONTH, lastDay);
* </pre>
*
* @return the last day-of-month adjuster, not null
*/
// 返回一个时间量整合器,该整合器通常是用temporal所在月的最后一天这个时间点来更新temporal的"天"部件
public static TemporalAdjuster lastDayOfMonth() {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 获取ChronoField.DAY_OF_MONTH字段的上区间最大值
long max = temporal.range(ChronoField.DAY_OF_MONTH).getMaximum();
return temporal.with(ChronoField.DAY_OF_MONTH, max);
}
};
}
/**
* Returns the "first day of next month" adjuster, which returns a new date set to
* the first day of the next month.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 will return 2011-02-01.<br>
* The input 2011-02-15 will return 2011-03-01.
* <p>
* The behavior is suitable for use with most calendar systems.
* It is equivalent to:
* <pre>
* temporal.with(DAY_OF_MONTH, 1).plus(1, MONTHS);
* </pre>
*
* @return the first day of next month adjuster, not null
*/
// 返回一个时间量整合器,该整合器通常是用temporal所在月的下个月的第一天这个时间点来更新temporal的"天"部件
public static TemporalAdjuster firstDayOfNextMonth() {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
return temporal.with(ChronoField.DAY_OF_MONTH, 1).plus(1, MONTHS);
}
};
}
/**
* Returns the "first day of year" adjuster, which returns a new date set to
* the first day of the current year.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 will return 2011-01-01.<br>
* The input 2011-02-15 will return 2011-01-01.<br>
* <p>
* The behavior is suitable for use with most calendar systems.
* It is equivalent to:
* <pre>
* temporal.with(DAY_OF_YEAR, 1);
* </pre>
*
* @return the first day-of-year adjuster, not null
*/
// 返回一个时间量整合器,该整合器通常是用temporal所在年的第一天这个时间点来更新temporal的"天"部件
public static TemporalAdjuster firstDayOfYear() {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
return temporal.with(ChronoField.DAY_OF_YEAR, 1);
}
};
}
/**
* Returns the "last day of year" adjuster, which returns a new date set to
* the last day of the current year.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 will return 2011-12-31.<br>
* The input 2011-02-15 will return 2011-12-31.<br>
* <p>
* The behavior is suitable for use with most calendar systems.
* It is equivalent to:
* <pre>
* long lastDay = temporal.range(DAY_OF_YEAR).getMaximum();
* temporal.with(DAY_OF_YEAR, lastDay);
* </pre>
*
* @return the last day-of-year adjuster, not null
*/
// 返回一个时间量整合器,该整合器通常是用temporal所在年的最后一天这个时间点来更新temporal的"天"部件
public static TemporalAdjuster lastDayOfYear() {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 获取ChronoField.DAY_OF_YEAR字段的上区间最大值
long max = temporal.range(ChronoField.DAY_OF_YEAR).getMaximum();
return temporal.with(ChronoField.DAY_OF_YEAR, max);
}
};
}
/**
* Returns the "first day of next year" adjuster, which returns a new date set to
* the first day of the next year.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 will return 2012-01-01.
* <p>
* The behavior is suitable for use with most calendar systems.
* It is equivalent to:
* <pre>
* temporal.with(DAY_OF_YEAR, 1).plus(1, YEARS);
* </pre>
*
* @return the first day of next month adjuster, not null
*/
// 返回一个时间量整合器,该整合器通常是用temporal所在年的下一年的第一天这个时间点来更新temporal的"天"部件
public static TemporalAdjuster firstDayOfNextYear() {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
return temporal.with(ChronoField.DAY_OF_YEAR, 1).plus(1, YEARS);
}
};
}
/**
* Returns the first in month adjuster, which returns a new date
* in the same month with the first matching day-of-week.
* This is used for expressions like 'first Tuesday in March'.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-12-15 for (MONDAY) will return 2011-12-05.<br>
* The input 2011-12-15 for (FRIDAY) will return 2011-12-02.<br>
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} and {@code DAY_OF_MONTH} fields
* and the {@code DAYS} unit, and assumes a seven day week.
*
* @param dayOfWeek the day-of-week, not null
*
* @return the first in month adjuster, not null
*/
// 返回一个时间量整合器,该整合器将temporal调节到当月第一天之后的第1个dayOfWeek时间点
public static TemporalAdjuster firstInMonth(DayOfWeek dayOfWeek) {
return TemporalAdjusters.dayOfWeekInMonth(1, dayOfWeek);
}
/**
* Returns the last in month adjuster, which returns a new date
* in the same month with the last matching day-of-week.
* This is used for expressions like 'last Tuesday in March'.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-12-15 for (MONDAY) will return 2011-12-26.<br>
* The input 2011-12-15 for (FRIDAY) will return 2011-12-30.<br>
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} and {@code DAY_OF_MONTH} fields
* and the {@code DAYS} unit, and assumes a seven day week.
*
* @param dayOfWeek the day-of-week, not null
*
* @return the first in month adjuster, not null
*/
// 返回一个时间量整合器,该整合器将temporal调节到当月最后一天之前的第1个dayOfWeek时间点
public static TemporalAdjuster lastInMonth(DayOfWeek dayOfWeek) {
return TemporalAdjusters.dayOfWeekInMonth(-1, dayOfWeek);
}
/**
* Returns the day-of-week in month adjuster, which returns a new date
* with the ordinal day-of-week based on the month.
* This is used for expressions like the 'second Tuesday in March'.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-12-15 for (1,TUESDAY) will return 2011-12-06.<br>
* The input 2011-12-15 for (2,TUESDAY) will return 2011-12-13.<br>
* The input 2011-12-15 for (3,TUESDAY) will return 2011-12-20.<br>
* The input 2011-12-15 for (4,TUESDAY) will return 2011-12-27.<br>
* The input 2011-12-15 for (5,TUESDAY) will return 2012-01-03.<br>
* The input 2011-12-15 for (-1,TUESDAY) will return 2011-12-27 (last in month).<br>
* The input 2011-12-15 for (-4,TUESDAY) will return 2011-12-06 (3 weeks before last in month).<br>
* The input 2011-12-15 for (-5,TUESDAY) will return 2011-11-29 (4 weeks before last in month).<br>
* The input 2011-12-15 for (0,TUESDAY) will return 2011-11-29 (last in previous month).<br>
* <p>
* For a positive or zero ordinal, the algorithm is equivalent to finding the first
* day-of-week that matches within the month and then adding a number of weeks to it.
* For a negative ordinal, the algorithm is equivalent to finding the last
* day-of-week that matches within the month and then subtracting a number of weeks to it.
* The ordinal number of weeks is not validated and is interpreted leniently
* according to this algorithm. This definition means that an ordinal of zero finds
* the last matching day-of-week in the previous month.
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} and {@code DAY_OF_MONTH} fields
* and the {@code DAYS} unit, and assumes a seven day week.
*
* @param ordinal the week within the month, unbounded but typically from -5 to 5
* @param dayOfWeek the day-of-week, not null
*
* @return the day-of-week in month adjuster, not null
*/
/*
* 返回一个时间量整合器来调节temporal。
*
* 如果ordinal>=0:
* > 先把temporal调节到当月第一天,
* > 再将temporal前进到后面第|ordinal|个dayOfWeek时间点。
* 如果ordinal为0,则是将temporal回退到前面第1个dayOfWeek时间点。
*
* 如果ordinal<0:
* > 先把temporal调节到当月最后一天,
* > 再将temporal回退到前面第|ordinal|个dayOfWeek时间点。
*/
public static TemporalAdjuster dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek) {
Objects.requireNonNull(dayOfWeek, "dayOfWeek");
// 返回dayOfWeek在一周中的天数,即周几
int dowValue = dayOfWeek.getValue();
if(ordinal >= 0) {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 用temporal所在月的第一天这个时间点来更新temporal的"天"部件
Temporal temp = temporal.with(ChronoField.DAY_OF_MONTH, 1);
// 获取temp当前是周几
int curDow = temp.get(ChronoField.DAY_OF_WEEK);
// temp到dayOfWeek的天数
int dowDiff = (dowValue - curDow + 7) % 7;
dowDiff += (ordinal - 1L) * 7L;
return temp.plus(dowDiff, DAYS);
}
};
} else {
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 获取ChronoField.DAY_OF_MONTH字段的上区间最大值
long max = temporal.range(ChronoField.DAY_OF_MONTH).getMaximum();
// 用temporal所在月的最后一天这个时间点来更新temporal的"天"部件
Temporal temp = temporal.with(ChronoField.DAY_OF_MONTH, max);
// 获取temp当前是周几
int curDow = temp.get(ChronoField.DAY_OF_WEEK);
// temp到dayOfWeek的天数
int daysDiff = dowValue - curDow;
daysDiff = (daysDiff == 0 ? 0 : (daysDiff>0 ? daysDiff - 7 : daysDiff));
daysDiff -= (-ordinal - 1L) * 7L;
return temp.plus(daysDiff, DAYS);
}
};
}
}
/**
* Returns the next day-of-week adjuster, which adjusts the date to the
* first occurrence of the specified day-of-week after the date being adjusted.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-17 (two days later).<br>
* The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-19 (four days later).<br>
* The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-22 (seven days later).
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
* and assumes a seven day week.
*
* @param dayOfWeek the day-of-week to move the date to, not null
*
* @return the next day-of-week adjuster, not null
*/
/*
* 返回一个时间量整合器,该整合器将temporal前进到下一个dayOfWeek时间点。
* 即使temporal已经在dayOfWeek这个时间点,仍需要加上7天进入下周。
*/
public static TemporalAdjuster next(DayOfWeek dayOfWeek) {
// 返回dayOfWeek在一周中的天数,即周几
int dowValue = dayOfWeek.getValue();
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 计算temporal是一周的第几天(周几)
int calDow = temporal.get(ChronoField.DAY_OF_WEEK);
// dayOfWeek到temporal的天数
int daysDiff = calDow - dowValue;
// 加上指定的天数,进入下一周
return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
}
};
}
/**
* Returns the next-or-same day-of-week adjuster, which adjusts the date to the
* first occurrence of the specified day-of-week after the date being adjusted
* unless it is already on that day in which case the same object is returned.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-17 (two days later).<br>
* The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-19 (four days later).<br>
* The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input).
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
* and assumes a seven day week.
*
* @param dayOfWeek the day-of-week to check for or move the date to, not null
*
* @return the next-or-same day-of-week adjuster, not null
*/
/*
* 返回一个时间量整合器,该整合器将temporal前进到下一个dayOfWeek时间点。
* 如果temporal已经在dayOfWeek这个时间点,则不做任何操作。
*/
public static TemporalAdjuster nextOrSame(DayOfWeek dayOfWeek) {
// 返回dayOfWeek在一周中的天数,即周几
int dowValue = dayOfWeek.getValue();
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 计算temporal是一周的第几天(周几)
int calDow = temporal.get(ChronoField.DAY_OF_WEEK);
if(calDow == dowValue) {
return temporal;
}
// dayOfWeek到temporal的天数
int daysDiff = calDow - dowValue;
// 加上指定的天数,进入下一周
return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
}
};
}
/**
* Returns the previous day-of-week adjuster, which adjusts the date to the
* first occurrence of the specified day-of-week before the date being adjusted.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
* The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
* The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-08 (seven days earlier).
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
* and assumes a seven day week.
*
* @param dayOfWeek the day-of-week to move the date to, not null
*
* @return the previous day-of-week adjuster, not null
*/
/*
* 返回一个时间量整合器,该整合器将temporal回退到上一个dayOfWeek时间点。
* 即使temporal已经在dayOfWeek这个时间点,仍需要减去7天进入上周。
*/
public static TemporalAdjuster previous(DayOfWeek dayOfWeek) {
// 返回dayOfWeek在一周中的天数,即周几
int dowValue = dayOfWeek.getValue();
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 计算temporal是一周的第几天(周几)
int calDow = temporal.get(ChronoField.DAY_OF_WEEK);
// temporal到dayOfWeek的天数
int daysDiff = dowValue - calDow;
// 减去指定的天数,进入上一周
return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
}
};
}
/**
* Returns the previous-or-same day-of-week adjuster, which adjusts the date to the
* first occurrence of the specified day-of-week before the date being adjusted
* unless it is already on that day in which case the same object is returned.
* <p>
* The ISO calendar system behaves as follows:<br>
* The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
* The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
* The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input).
* <p>
* The behavior is suitable for use with most calendar systems.
* It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
* and assumes a seven day week.
*
* @param dayOfWeek the day-of-week to check for or move the date to, not null
*
* @return the previous-or-same day-of-week adjuster, not null
*/
/*
* 返回一个时间量整合器,该整合器将temporal回退到上一个dayOfWeek时间点。
* 如果temporal已经在dayOfWeek这个时间点,则不做任何操作。
*/
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek) {
// 返回dayOfWeek在一周中的天数,即周几
int dowValue = dayOfWeek.getValue();
return new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
// 计算temporal是一周的第几天(周几)
int calDow = temporal.get(ChronoField.DAY_OF_WEEK);
if(calDow == dowValue) {
return temporal;
}
// temporal到dayOfWeek的天数
int daysDiff = dowValue - calDow;
// 减去指定的天数,进入上一周
return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
}
};
}
}