gpt4 book ai didi

Java日历添加不递增年份

转载 作者:行者123 更新时间:2023-12-02 06:16:03 29 4
gpt4 key购买 nike

我有一个存储周开始日期的变量,但是当我从 2012 年前进到 2013 年时,它不会更新年份。

日期和月份添加正确,因此它正确地从 26/12 到 02/01,但仍保留为 2012 年。

// roll one week forward
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mPrefsHelper.getWeekStart());
cal.add(Calendar.WEEK_OF_YEAR, 1);

最佳答案

来自java.util.Calendar的javadoc

First Week

Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in firstweek (from 1 to 7). These numbers are taken from the locale resourcedata when a Calendar is constructed. They may also be specifiedexplicitly through the methods for setting their values.

When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields, Calendar must determine the first week of the month or year as areference point. The first week of a month or year is defined as theearliest seven day period beginning on getFirstDayOfWeek() andcontaining at least getMinimalDaysInFirstWeek() days of that month oryear. Weeks numbered ..., -1, 0 precede the first week; weeks numbered2, 3,... follow it. Note that the normalized numbering returned byget() may be different. For example, a specific Calendar subclass maydesignate the week before week 1 of a year as week n of the previousyear.

它是特定于区域设置的。在您的情况下,如果该周包含新年后的天数,则将其计为新年后的第 1 周。

关于Java日历添加不递增年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019893/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com