Why does java.time.Period#normalized() not normalize days?What is reflection and why is it useful?Does a finally block always get executed in Java?What is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is processing a sorted array faster than processing an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?
How is the entropy at the time of the Big Bang calculated?
Is part of the Mediterranean sea considered Eretz Israel?
How to check whether the permutation is random or not
Is it usual for a US president to make specific comments about a UK Prime Minister's suitability during a general election?
COUNTIFS - How do I match text with < and > characters
Are the graphical depictions in docs drawed in manual labour?
Should plywood be missing on my roof?
Does Amoxicillin (as trihydrate) degrade in stomach acid?
I have been accused of copying two lab reports from the previous year even though I had done everything by myself
Golf Lathe Machine
Mechanics to keep mobs and environment alive without using tons of memory?
Options for installing sub-panel
Single word for delaying an unpleasant task
How can I use Callout to label the segments of a piecewise function?
Why are dates (of writing or publication) missing in scientific papers?
Drying clothes: windy but cold outside vs warm(ish) inside?
Why do most flyback converters / LED drivers operate under 100 kHz?
Are medical malpractice lawsuits part of the public record?
If a creature has advantage against spells and magical effects, is it resistant to magical attacks?
A professor commented that my research is too simple as compared to my colleagues. What does that mean about my future prospects?
Why does the single dot entry exist in file systems?
Why is Trump moving to Florida?
Short story about a young psychic man who disrupts a home full of unusual people?
What were the prevailing real-life cultural connotations of "clones" in 1975 (Star Wars' 3rd draft)?
Why does java.time.Period#normalized() not normalize days?
What is reflection and why is it useful?Does a finally block always get executed in Java?What is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is processing a sorted array faster than processing an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
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?
java datetime java-time iso8601 period
add a comment
|
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?
java datetime java-time iso8601 period
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
Jul 13 at 17:52
add a comment
|
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?
java datetime java-time iso8601 period
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?
java datetime java-time iso8601 period
java datetime java-time iso8601 period
edited Jul 13 at 18:46
Basil Bourque
140k38 gold badges466 silver badges652 bronze badges
140k38 gold badges466 silver badges652 bronze badges
asked Jul 13 at 17:26
D. KovácsD. Kovács
9336 silver badges19 bronze badges
9336 silver badges19 bronze badges
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
Jul 13 at 17:52
add a comment
|
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
Jul 13 at 17:52
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
Jul 13 at 17:52
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
Jul 13 at 17:52
add a comment
|
1 Answer
1
active
oldest
votes
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.
1
That makes sense. But this means, that the "day" part of aPeriod
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 acceptsPeriod
objects, you must essentially sanity-check them?)
– D. Kovács
Jul 13 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 at 18:24
Against malicious input... theoretical API for settings validity of max 2 years accepts aPeriod
. 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 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 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.
– Max Vollmer
Oct 28 at 19:04
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57021385%2fwhy-does-java-time-periodnormalized-not-normalize-days%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
That makes sense. But this means, that the "day" part of aPeriod
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 acceptsPeriod
objects, you must essentially sanity-check them?)
– D. Kovács
Jul 13 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 at 18:24
Against malicious input... theoretical API for settings validity of max 2 years accepts aPeriod
. 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 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 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.
– Max Vollmer
Oct 28 at 19:04
|
show 3 more comments
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.
1
That makes sense. But this means, that the "day" part of aPeriod
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 acceptsPeriod
objects, you must essentially sanity-check them?)
– D. Kovács
Jul 13 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 at 18:24
Against malicious input... theoretical API for settings validity of max 2 years accepts aPeriod
. 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 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 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.
– Max Vollmer
Oct 28 at 19:04
|
show 3 more comments
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.
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.
answered Jul 13 at 17:43
Max VollmerMax Vollmer
7,5966 gold badges22 silver badges40 bronze badges
7,5966 gold badges22 silver badges40 bronze badges
1
That makes sense. But this means, that the "day" part of aPeriod
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 acceptsPeriod
objects, you must essentially sanity-check them?)
– D. Kovács
Jul 13 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 at 18:24
Against malicious input... theoretical API for settings validity of max 2 years accepts aPeriod
. 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 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 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.
– Max Vollmer
Oct 28 at 19:04
|
show 3 more comments
1
That makes sense. But this means, that the "day" part of aPeriod
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 acceptsPeriod
objects, you must essentially sanity-check them?)
– D. Kovács
Jul 13 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 at 18:24
Against malicious input... theoretical API for settings validity of max 2 years accepts aPeriod
. 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 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 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.
– Max Vollmer
Oct 28 at 19:04
1
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 at 17:56
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 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 at 18:24
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 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 at 18:49
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 at 18:49
2
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 at 19:42
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 at 19:42
1
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 at 19:04
@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 at 19:04
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57021385%2fwhy-does-java-time-periodnormalized-not-normalize-days%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
Jul 13 at 17:52