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 “#”?
Create dashed intersections with labels using pgfplots and tikz
Are there any double stars that I can actually see orbit each other?
How can I legally visit the United States Minor Outlying Islands in the Pacific?
What would be the ideal melee weapon made of "Phase Metal"?
How can an advanced civilization forget how to manufacture its technology?
Installing ubuntu with HD + SSD
How to repair a laptop's screen hinges?
A DVR algebra with weird automorphisms
Why does Hellboy file down his horns?
Why is dry soil hydrophobic? Bad gardener paradox
What is this welding tool I found in my attic?
Can I intentionally omit previous work experience or pretend it doesn't exist when applying for jobs?
Metric version of "footage"?
wavelength of seismic wave with a gaussian source
Why does the autopilot disengage even when it does not receive pilot input?
Doing research in academia and not liking competition
Can I play a first turn Simic Growth Chamber to have 3 mana available in the second turn?
Optimizing Process Builder: Early Exit: Worthwhile?
Cubic programming and beyond?
Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?
Are lithium batteries allowed in the International Space Station?
How does one stock fund's charge of 1% more in operating expenses than another fund lower expected returns by 10%?
Filtering fine silt/mud from water (not necessarily bacteria etc.)
Is this floating-point optimization allowed?
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
8 hours ago
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 7 hours ago
Basil Bourque
127k36 gold badges427 silver badges600 bronze badges
127k36 gold badges427 silver badges600 bronze badges
asked 9 hours ago
D. KovácsD. Kovács
8255 silver badges19 bronze badges
8255 silver badges19 bronze badges
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
8 hours ago
add a comment |
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
8 hours ago
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
8 hours ago
@Zabuza: I'm familiar with the Wikipedia page, however, it does not cover normalization of dates/periods.
– D. Kovács
8 hours ago
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
8 hours ago
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
8 hours ago
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
7 hours ago
1
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
6 hours ago
add a comment |
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/3.0/"u003ecc by-sa 3.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
8 hours ago
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
8 hours ago
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
7 hours ago
1
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
6 hours ago
add a comment |
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
8 hours ago
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
8 hours ago
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
7 hours ago
1
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
6 hours ago
add a comment |
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 8 hours ago
Max VollmerMax Vollmer
6,3745 gold badges21 silver badges38 bronze badges
6,3745 gold badges21 silver badges38 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
8 hours ago
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
8 hours ago
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
7 hours ago
1
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
6 hours ago
add a comment |
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
8 hours ago
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
8 hours ago
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
7 hours ago
1
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
6 hours ago
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
8 hours ago
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
8 hours ago
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
8 hours ago
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
8 hours ago
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
7 hours ago
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
7 hours ago
1
1
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
6 hours ago
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
6 hours ago
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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
8 hours ago