Add 1 Week to a Date, which way is preferred?

前端 未结 10 1698
迷失自我
迷失自我 2021-01-07 17:30

I am reviewing some code at work and came across an inconsistency in how the code handles adding 1 week to the current time and was wondering if there was any reason why one

10条回答
  •  别那么骄傲
    2021-01-07 17:58

    The two methods might give different results when a change in daylight saving time is involved. Imagine the current time is 23:50 and at 02:00 the clock jumps to 03:00. When you just add 7 days in milliseconds the time would be 00:50 on the following day. Addings 7 days, the resulting time would still be 23:50.

    To make the confusion complete, you could also try add(Calendar.WEEK_OF_YEAR, 1), not sure how that would differ though.

提交回复
热议问题