Difference between adding Duration.ofDays(1) and Period.ofDays(1) to ZonedDateTime around Daylight Savings...












2















The Daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. As a result, 2 AM becomes 1 AM.



I am not able to understand the following in the code given below:




  1. Why line 2 shows time 09:00, why not 10:00 (by adding 1 day)?


  2. Why line 4 shows time 10:00, why not 09:00 (by adding 24 hours)?



    LocalDateTime ld = LocalDateTime.of(2015, Month.OCTOBER, 31, 10, 0);

    ZonedDateTime date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 1 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Duration.ofDays(1));
    System.out.println(date); //line 2 - 2015-11-01T09:00-05:00[US/Eastern]

    date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 3 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Period.ofDays(1));
    System.out.println(date); //line 4 - 2015-11-01T10:00-05:00[US/Eastern]



Could somebody please help me with it?










share|improve this question

























  • 1 day and 24 hours are two different things.

    – Basil Bourque
    Nov 14 '18 at 21:16
















2















The Daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. As a result, 2 AM becomes 1 AM.



I am not able to understand the following in the code given below:




  1. Why line 2 shows time 09:00, why not 10:00 (by adding 1 day)?


  2. Why line 4 shows time 10:00, why not 09:00 (by adding 24 hours)?



    LocalDateTime ld = LocalDateTime.of(2015, Month.OCTOBER, 31, 10, 0);

    ZonedDateTime date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 1 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Duration.ofDays(1));
    System.out.println(date); //line 2 - 2015-11-01T09:00-05:00[US/Eastern]

    date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 3 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Period.ofDays(1));
    System.out.println(date); //line 4 - 2015-11-01T10:00-05:00[US/Eastern]



Could somebody please help me with it?










share|improve this question

























  • 1 day and 24 hours are two different things.

    – Basil Bourque
    Nov 14 '18 at 21:16














2












2








2


1






The Daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. As a result, 2 AM becomes 1 AM.



I am not able to understand the following in the code given below:




  1. Why line 2 shows time 09:00, why not 10:00 (by adding 1 day)?


  2. Why line 4 shows time 10:00, why not 09:00 (by adding 24 hours)?



    LocalDateTime ld = LocalDateTime.of(2015, Month.OCTOBER, 31, 10, 0);

    ZonedDateTime date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 1 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Duration.ofDays(1));
    System.out.println(date); //line 2 - 2015-11-01T09:00-05:00[US/Eastern]

    date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 3 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Period.ofDays(1));
    System.out.println(date); //line 4 - 2015-11-01T10:00-05:00[US/Eastern]



Could somebody please help me with it?










share|improve this question
















The Daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. As a result, 2 AM becomes 1 AM.



I am not able to understand the following in the code given below:




  1. Why line 2 shows time 09:00, why not 10:00 (by adding 1 day)?


  2. Why line 4 shows time 10:00, why not 09:00 (by adding 24 hours)?



    LocalDateTime ld = LocalDateTime.of(2015, Month.OCTOBER, 31, 10, 0);

    ZonedDateTime date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 1 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Duration.ofDays(1));
    System.out.println(date); //line 2 - 2015-11-01T09:00-05:00[US/Eastern]

    date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 3 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Period.ofDays(1));
    System.out.println(date); //line 4 - 2015-11-01T10:00-05:00[US/Eastern]



Could somebody please help me with it?







java datetime java-8 dst zoneddatetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 4:20









Ole V.V.

28.5k63352




28.5k63352










asked Nov 13 '18 at 18:52









skipskip

4,9162187137




4,9162187137













  • 1 day and 24 hours are two different things.

    – Basil Bourque
    Nov 14 '18 at 21:16



















  • 1 day and 24 hours are two different things.

    – Basil Bourque
    Nov 14 '18 at 21:16

















1 day and 24 hours are two different things.

– Basil Bourque
Nov 14 '18 at 21:16





1 day and 24 hours are two different things.

– Basil Bourque
Nov 14 '18 at 21:16












2 Answers
2






active

oldest

votes


















4














Duration: Despite the ofDays method Duration hasn’t got a notion of days. Duration.ofDays(1) is immediately converted into 24 hours, so this is what you are adding. Since you add 24 hours to 10:00 the day before DST ends, you get 09:00 the next day as you have observed,



Period: Contrary to Duration a Period knows days, weeks, months and years. So you are adding 1 calendar day, hitting the same wall clock time on the next day (10:00) even though this means 25 hours later (not 24).






share|improve this answer


























  • If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

    – skip
    Nov 14 '18 at 7:03






  • 2





    Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

    – Ole V.V.
    Nov 14 '18 at 7:09













  • You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

    – skip
    Nov 14 '18 at 7:29





















4














See the full Java documentation on Duration and Period, always one quick Google search away.



From Duration:




In addition, the DAYS unit can be used and is treated as exactly equal
to 24 hours, thus ignoring daylight savings effects. See Period for
the date-based equivalent to this class.




From Period:




Durations and periods differ in their treatment of daylight savings
time when added to ZonedDateTime. A Duration will add an exact number
of seconds, thus a duration of one day is always exactly 24 hours. By
contrast, a Period will add a conceptual day, trying to maintain the
local time




So, Period will maintain the same hour, whereas Duration will add 24 hours.



24 hours after 10:00 on October 31 is 9:00 on November 1st.




  1. 11:00

  2. 12:00

  3. 13:00 (1 pm)

  4. 14:00

  5. 15:00

  6. 16:00

  7. 17:00

  8. 18:00

  9. 19:00

  10. 20:00

  11. 21:00

  12. 22:00

  13. 23:00

  14. 0:00 (midnight)

  15. 1:00

  16. 1:00 (here's that extra hour)

  17. 2:00

  18. 3:00

  19. 4:00

  20. 5:00

  21. 6:00

  22. 7:00

  23. 8:00

  24. 9:00






share|improve this answer



















  • 1





    Here too you’ve been confused, @skip. :-) The answer is correct.

    – Ole V.V.
    Nov 14 '18 at 7:39













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287725%2fdifference-between-adding-duration-ofdays1-and-period-ofdays1-to-zoneddateti%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Duration: Despite the ofDays method Duration hasn’t got a notion of days. Duration.ofDays(1) is immediately converted into 24 hours, so this is what you are adding. Since you add 24 hours to 10:00 the day before DST ends, you get 09:00 the next day as you have observed,



Period: Contrary to Duration a Period knows days, weeks, months and years. So you are adding 1 calendar day, hitting the same wall clock time on the next day (10:00) even though this means 25 hours later (not 24).






share|improve this answer


























  • If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

    – skip
    Nov 14 '18 at 7:03






  • 2





    Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

    – Ole V.V.
    Nov 14 '18 at 7:09













  • You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

    – skip
    Nov 14 '18 at 7:29


















4














Duration: Despite the ofDays method Duration hasn’t got a notion of days. Duration.ofDays(1) is immediately converted into 24 hours, so this is what you are adding. Since you add 24 hours to 10:00 the day before DST ends, you get 09:00 the next day as you have observed,



Period: Contrary to Duration a Period knows days, weeks, months and years. So you are adding 1 calendar day, hitting the same wall clock time on the next day (10:00) even though this means 25 hours later (not 24).






share|improve this answer


























  • If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

    – skip
    Nov 14 '18 at 7:03






  • 2





    Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

    – Ole V.V.
    Nov 14 '18 at 7:09













  • You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

    – skip
    Nov 14 '18 at 7:29
















4












4








4







Duration: Despite the ofDays method Duration hasn’t got a notion of days. Duration.ofDays(1) is immediately converted into 24 hours, so this is what you are adding. Since you add 24 hours to 10:00 the day before DST ends, you get 09:00 the next day as you have observed,



Period: Contrary to Duration a Period knows days, weeks, months and years. So you are adding 1 calendar day, hitting the same wall clock time on the next day (10:00) even though this means 25 hours later (not 24).






share|improve this answer















Duration: Despite the ofDays method Duration hasn’t got a notion of days. Duration.ofDays(1) is immediately converted into 24 hours, so this is what you are adding. Since you add 24 hours to 10:00 the day before DST ends, you get 09:00 the next day as you have observed,



Period: Contrary to Duration a Period knows days, weeks, months and years. So you are adding 1 calendar day, hitting the same wall clock time on the next day (10:00) even though this means 25 hours later (not 24).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 7:08

























answered Nov 14 '18 at 4:37









Ole V.V.Ole V.V.

28.5k63352




28.5k63352













  • If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

    – skip
    Nov 14 '18 at 7:03






  • 2





    Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

    – Ole V.V.
    Nov 14 '18 at 7:09













  • You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

    – skip
    Nov 14 '18 at 7:29





















  • If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

    – skip
    Nov 14 '18 at 7:03






  • 2





    Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

    – Ole V.V.
    Nov 14 '18 at 7:09













  • You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

    – skip
    Nov 14 '18 at 7:29



















If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

– skip
Nov 14 '18 at 7:03





If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Similarly, should adding a day (for period of 1 day) give us 10:00 next day? What I am getting in the answer is other way round. Duration.ofDays(1) is giving 10:00, while Period.ofDays(1) is giving 09:00.

– skip
Nov 14 '18 at 7:03




2




2





Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

– Ole V.V.
Nov 14 '18 at 7:09







Aren’t you confusing yourself now? If duration adds 24 hours, should it not give 09:00 next day (considering DST)? Yes, and this is also what you report in the question that you get. Similarly, should adding a day (for period of 1 day) give us 10:00 next day? Yes, and your question states that this is exactly what you got.

– Ole V.V.
Nov 14 '18 at 7:09















You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

– skip
Nov 14 '18 at 7:29







You're correct. It looks like I've been seeing and reading Period.ofDays(1) as Duration.ofDays(1) and Duration.ofDays(1) as Period.ofDays(1). Even in my question I did the same. I thought line 2 was for Period and line 4 for Duration. Thanks.

– skip
Nov 14 '18 at 7:29















4














See the full Java documentation on Duration and Period, always one quick Google search away.



From Duration:




In addition, the DAYS unit can be used and is treated as exactly equal
to 24 hours, thus ignoring daylight savings effects. See Period for
the date-based equivalent to this class.




From Period:




Durations and periods differ in their treatment of daylight savings
time when added to ZonedDateTime. A Duration will add an exact number
of seconds, thus a duration of one day is always exactly 24 hours. By
contrast, a Period will add a conceptual day, trying to maintain the
local time




So, Period will maintain the same hour, whereas Duration will add 24 hours.



24 hours after 10:00 on October 31 is 9:00 on November 1st.




  1. 11:00

  2. 12:00

  3. 13:00 (1 pm)

  4. 14:00

  5. 15:00

  6. 16:00

  7. 17:00

  8. 18:00

  9. 19:00

  10. 20:00

  11. 21:00

  12. 22:00

  13. 23:00

  14. 0:00 (midnight)

  15. 1:00

  16. 1:00 (here's that extra hour)

  17. 2:00

  18. 3:00

  19. 4:00

  20. 5:00

  21. 6:00

  22. 7:00

  23. 8:00

  24. 9:00






share|improve this answer



















  • 1





    Here too you’ve been confused, @skip. :-) The answer is correct.

    – Ole V.V.
    Nov 14 '18 at 7:39


















4














See the full Java documentation on Duration and Period, always one quick Google search away.



From Duration:




In addition, the DAYS unit can be used and is treated as exactly equal
to 24 hours, thus ignoring daylight savings effects. See Period for
the date-based equivalent to this class.




From Period:




Durations and periods differ in their treatment of daylight savings
time when added to ZonedDateTime. A Duration will add an exact number
of seconds, thus a duration of one day is always exactly 24 hours. By
contrast, a Period will add a conceptual day, trying to maintain the
local time




So, Period will maintain the same hour, whereas Duration will add 24 hours.



24 hours after 10:00 on October 31 is 9:00 on November 1st.




  1. 11:00

  2. 12:00

  3. 13:00 (1 pm)

  4. 14:00

  5. 15:00

  6. 16:00

  7. 17:00

  8. 18:00

  9. 19:00

  10. 20:00

  11. 21:00

  12. 22:00

  13. 23:00

  14. 0:00 (midnight)

  15. 1:00

  16. 1:00 (here's that extra hour)

  17. 2:00

  18. 3:00

  19. 4:00

  20. 5:00

  21. 6:00

  22. 7:00

  23. 8:00

  24. 9:00






share|improve this answer



















  • 1





    Here too you’ve been confused, @skip. :-) The answer is correct.

    – Ole V.V.
    Nov 14 '18 at 7:39
















4












4








4







See the full Java documentation on Duration and Period, always one quick Google search away.



From Duration:




In addition, the DAYS unit can be used and is treated as exactly equal
to 24 hours, thus ignoring daylight savings effects. See Period for
the date-based equivalent to this class.




From Period:




Durations and periods differ in their treatment of daylight savings
time when added to ZonedDateTime. A Duration will add an exact number
of seconds, thus a duration of one day is always exactly 24 hours. By
contrast, a Period will add a conceptual day, trying to maintain the
local time




So, Period will maintain the same hour, whereas Duration will add 24 hours.



24 hours after 10:00 on October 31 is 9:00 on November 1st.




  1. 11:00

  2. 12:00

  3. 13:00 (1 pm)

  4. 14:00

  5. 15:00

  6. 16:00

  7. 17:00

  8. 18:00

  9. 19:00

  10. 20:00

  11. 21:00

  12. 22:00

  13. 23:00

  14. 0:00 (midnight)

  15. 1:00

  16. 1:00 (here's that extra hour)

  17. 2:00

  18. 3:00

  19. 4:00

  20. 5:00

  21. 6:00

  22. 7:00

  23. 8:00

  24. 9:00






share|improve this answer













See the full Java documentation on Duration and Period, always one quick Google search away.



From Duration:




In addition, the DAYS unit can be used and is treated as exactly equal
to 24 hours, thus ignoring daylight savings effects. See Period for
the date-based equivalent to this class.




From Period:




Durations and periods differ in their treatment of daylight savings
time when added to ZonedDateTime. A Duration will add an exact number
of seconds, thus a duration of one day is always exactly 24 hours. By
contrast, a Period will add a conceptual day, trying to maintain the
local time




So, Period will maintain the same hour, whereas Duration will add 24 hours.



24 hours after 10:00 on October 31 is 9:00 on November 1st.




  1. 11:00

  2. 12:00

  3. 13:00 (1 pm)

  4. 14:00

  5. 15:00

  6. 16:00

  7. 17:00

  8. 18:00

  9. 19:00

  10. 20:00

  11. 21:00

  12. 22:00

  13. 23:00

  14. 0:00 (midnight)

  15. 1:00

  16. 1:00 (here's that extra hour)

  17. 2:00

  18. 3:00

  19. 4:00

  20. 5:00

  21. 6:00

  22. 7:00

  23. 8:00

  24. 9:00







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 20:24









Wonko the SaneWonko the Sane

8,70964978




8,70964978








  • 1





    Here too you’ve been confused, @skip. :-) The answer is correct.

    – Ole V.V.
    Nov 14 '18 at 7:39
















  • 1





    Here too you’ve been confused, @skip. :-) The answer is correct.

    – Ole V.V.
    Nov 14 '18 at 7:39










1




1





Here too you’ve been confused, @skip. :-) The answer is correct.

– Ole V.V.
Nov 14 '18 at 7:39







Here too you’ve been confused, @skip. :-) The answer is correct.

– Ole V.V.
Nov 14 '18 at 7:39




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287725%2fdifference-between-adding-duration-ofdays1-and-period-ofdays1-to-zoneddateti%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation