9

In the Java class java.time.Period the method normalized() has the following in its Javadoc:

This normalizes the years and months units, leaving the days unit unchanged.

The superclass' method has the following in its Javadoc:

The process of normalization is specific to each calendar system. For example, in the ISO calendar system, the years and months are normalized but the days are not, [...]

I do not have access to the actual text of ISO 8601-1:2019, and would not like to spend hundreds of [insert currency here]s on it (my guess is that normalization may be described in Part 1: Basic rules and not in Part 2: Extensions).

Could someone shed light upon why Period#normalized() does not normalize days? Does it really come directly from ISO 8601 itself, is it somewhere else specified, or is it just specific to the Java implementation?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
D. Kovács
  • 1,232
  • 13
  • 25

1 Answers1

11

This is because a period of years or months is always the same amount of time (the same period) for any given date. A year is always 12 months, 12 months are always a year, thus these parts of the period can easily be normalized.

However days are variable in relation to months and years. If you have a period of 1 year, 1 month and 32 days, you cannot normalize this to 1 year, 2 months and then a fixed amount of days, because it might be 1 day, 2 days, 3 days or 4 days, depending on which date you will apply the period on.

A month can be 28, 29, 30 or 31 days. A year can be 365 or 366 days. And since a period is independent of any fixed date, there is no way to decide these relations.

Example:

2019-01-01 + 01-01-32 is 2020-03-04

2020-01-01 + 01-01-32 is 2021-03-03

2020-02-01 + 01-01-32 is 2021-04-02

2020-03-01 + 01-01-32 is 2021-05-03

As you can see the days resulting from applying the same period to different dates varies depending on the month and on if it's a leap year.

Thus it is impossible to normalize days in a period and the days are not touched when normalizing.

Community
  • 1
  • 1
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • 1
    That makes sense. But this means, that the "day" part of a `Period` can be anything between Integer.MIN_VALUE and Integer.MAX_VALUE making some quite interesting constellations possible even after normalization, right? (I.e., if your API accepts `Period` objects, you must essentially sanity-check them?) – D. Kovács Jul 13 '19 at 17:56
  • Yeah, they can take any value. I don't see why that needs to be sanitized. A period of 500000 days is a valid period, why wouldn't it be? – Max Vollmer Jul 13 '19 at 18:24
  • Against malicious input... theoretical API for settings validity of max 2 years accepts a `Period`. Malicious input is 0 years, 1 month, 3650 days. You normalize and check year <= 2 and months accordingly. Yet, you set 10 years and a 1 month. (I know, it's a stretch, but you get the idea.) – D. Kovács Jul 13 '19 at 18:49
  • 2
    I would check the date I get as a result from using the period, not the period itself. But I guess it really depends on what the API is actually doing. – Max Vollmer Jul 13 '19 at 19:42
  • Coming back here a couple months later... actually... can you normalize months into years? Almost every four years you have an extra day because of leaping. This means that if your period contains between 28*4*12==1344 and 31*4*12==1488 months, then you should not normalize months into years. Because you don't know whether a non-leap year (e.g., 2000) was in your period or not, and thus you don't know whether you should add an extra month or not. (If the logic is sound, that for every 1488 months, if there is no non-leap year, you should add an extra month because of leap-days). – D. Kovács Oct 28 '19 at 17:37
  • @D.Kovács Huh? The amount of months doesn't change in a leap year. A year is always 12 months no matter what. I don't understand what `28*4*12` and `31*4*12` are supposed to mean. – Max Vollmer Oct 28 '19 at 17:55
  • If you have 1488 months those span 124 years, which contain at most 31 leap years. A leap year has an extra day. Thus in 1488 months you have at most 31 extra days, thus an extra month (if no non-leap year like 2000 is in those 124 years). – D. Kovács Oct 28 '19 at 18:00
  • 1
    @D.Kovács No you don't. There are no extra months. It doesn't matter how many days these months have. 31 leap days are not an extra month, they are just 31 months with a leap day. You are trying to normalize days again. – Max Vollmer Oct 28 '19 at 19:04