@@ -24,16 +24,20 @@ final class LocalDate private (year: Int, month: Month, dayOfMonth: Int)
24
24
25
25
private lazy val dayOfYear =
26
26
month.firstDayOfYear(_isLeapYear) + dayOfMonth - 1
27
+
27
28
private lazy val epochDay = {
28
29
val year1 = year - 1970
29
30
val daysBeforeYear = daysBeforeYears(Math .floorMod(year1, 400 ))._2
30
31
val offset =
31
32
Math .floorDiv(year1, 400 ).toLong * daysInFourHundredYears
32
33
offset + daysBeforeYear + dayOfYear - 1
33
34
}
35
+
34
36
private lazy val dayOfWeek =
35
37
Math .floorMod(epochDay + 3 , 7 ).toInt + 1
36
38
39
+ private def prolepticMonth = year.toLong * 12 + getMonthValue - 1
40
+
37
41
// Implemented by ChronoLocalDate
38
42
// def isSupported(field: TemporalField): Boolean
39
43
// def isSupported(unit: TemporalUnit): Boolean
@@ -64,7 +68,7 @@ final class LocalDate private (year: Int, month: Month, dayOfMonth: Int)
64
68
case ALIGNED_WEEK_OF_MONTH => (dayOfMonth - 1 ) / 7 + 1
65
69
case ALIGNED_WEEK_OF_YEAR => (dayOfYear - 1 ) / 7 + 1
66
70
case MONTH_OF_YEAR => getMonthValue
67
- case PROLEPTIC_MONTH => year.toLong * 12 + getMonthValue - 1
71
+ case PROLEPTIC_MONTH => prolepticMonth
68
72
case YEAR_OF_ERA => if (year > 0 ) year else 1 - year
69
73
case YEAR => year
70
74
case ERA => if (year > 0 ) 1 else 0
@@ -271,8 +275,7 @@ final class LocalDate private (year: Int, month: Month, dayOfMonth: Int)
271
275
case WEEKS => (other.toEpochDay - epochDay) / 7
272
276
273
277
case MONTHS =>
274
- val dmonths =
275
- (other.getYear - year).toLong * 12 + other.getMonthValue - getMonthValue
278
+ val dmonths = other.prolepticMonth - prolepticMonth
276
279
if (other.getDayOfMonth < dayOfMonth && dmonths > 0 ) dmonths - 1
277
280
else if (other.getDayOfMonth > dayOfMonth && dmonths < 0 ) dmonths + 1
278
281
else dmonths
@@ -307,17 +310,22 @@ final class LocalDate private (year: Int, month: Month, dayOfMonth: Int)
307
310
308
311
def until (end : ChronoLocalDate ): Period = {
309
312
val other = LocalDate .from(end)
310
- if (equals(other)) Period .ZERO
311
- else if (isBefore(other)) {
312
- val months = until(other, MONTHS )
313
- val date1 = plus(months, MONTHS )
314
- val days = date1.until(other, DAYS ).toInt
315
- val years = (months / 12 ).toInt
316
- val months1 = (months % 12 ).toInt
317
- Period .of(years, months1, days)
318
- } else {
319
- other.until(this ).negated
313
+ val dmonths = other.prolepticMonth - prolepticMonth
314
+ val ddays = other.getDayOfMonth - dayOfMonth
315
+ val corr = {
316
+ if (dmonths > 0 && ddays < 0 ) - 1
317
+ else if (dmonths < 0 && ddays > 0 ) 1
318
+ else 0
319
+ }
320
+ val months = dmonths + corr
321
+ val days = {
322
+ if (corr < 0 ) plus(months, MONTHS ).until(other, DAYS ).toInt
323
+ else if (corr > 0 ) ddays - other.lengthOfMonth
324
+ else ddays
320
325
}
326
+ val years = (months / 12 ).toInt
327
+ val months1 = (months % 12 ).toInt
328
+ Period .of(years, months1, days)
321
329
}
322
330
323
331
// Not implemented
0 commit comments