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?

1
  • @Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
    – D. Kovács
    Jul 13, 2019 at 17:52

1 Answer 1

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.

8
  • 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, 2019 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? Jul 13, 2019 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, 2019 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. Jul 13, 2019 at 19:42
  • 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. Oct 28, 2019 at 19:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.