简体   繁体   English

获取一周的一周,使用java日历将星期日作为一周的第一天

[英]Get the week of year, with sunday as the first day of the week using java calendar

I'm trying to generate some code for our financial department, and they require a field that will give them the current week of the year, but based off of Sunday being the first day of the week. 我正在尝试为我们的财务部门生成一些代码,并且他们需要一个字段来为他们提供当年的当前周,但是基于星期日是一周的第一天。 So for instance 01-25-16 (MM-dd-yyyy) would be week 4. However when I try it using java.util.Calendar and calendar.setFirstDayOfWeek to Sunday, it tells me that this is week 5 because it seems to be counting from the end of December to the 3rd of January as week 1. 所以例如01-25-16(MM-dd-yyyy)将是第4周。但是当我尝试使用java.util.Calendarcalendar.setFirstDayOfWeek到星期日时,它告诉我这是第5周因为它似乎计算从12月底到1月3日为第1周。

Here is the code I've written so far: 这是我到目前为止编写的代码:

private static int getWeekOfYearBySunday(DateTime dt){
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(dt.getMillis());
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);

    return calendar.get(Calendar.WEEK_OF_YEAR);
}

This returns a week number of 5, even though it should be 4 if basing the start of the week on Sunday. 这将返回一个5的周数,即使它在星期日基于星期一开始应该是4。

You have to change minimalDaysInFirstWeek , see Calendar#setMinimalDaysInFirstWeek(int) : 您必须更改minimalDaysInFirstWeek ,请参阅Calendar#setMinimalDaysInFirstWeek(int)

Sets what the minimal days required in the first week of the year are; 设定一年中第一周所需的最小天数; For example, if the first week is defined as one that contains the first day of the first month of a year, call this method with value 1. If it must be a full week, use value 7. 例如,如果第一周定义为包含一年中第一个月的第一天的那一周,则使用值1调用此方法。如果它必须是整周,则使用值7。

See also GregorianCalendar : 另见GregorianCalendar

For example, January 1, 1998 is a Thursday. 例如,1998年1月1日是星期四。 If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY , then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; 如果getFirstDayOfWeek()MONDAY并且getMinimalDaysInFirstWeek()是4(ISO 8601标准兼容设置),那么1998年第1周将于1997年12月29日开始,并于1998年1月4日结束。过去三天的一周是1998年但是,如果getFirstDayOfWeek()SUNDAY ,则1998年第1周从1998年1月4日开始,到1998年1月10日结束; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997. 1998年的前三天是1997年第53周的一部分,其一周是1997年。

But if you code for your financial department , you should rather create your calendar with the right Locale instead of changing some properties, see Calendar#getInstance(Locale) : 但是,如果您为财务部门编写代码,则应该使用正确的Locale创建日历,而不是更改某些属性,请参阅Calendar#getInstance(Locale)

Gets a calendar using the default time zone and specified locale. 使用默认时区和指定的区域设置获取日历。 The Calendar returned is based on the current time in the default time zone with the given locale. 返回的Calendar基于具有给定语言环境的默认时区中的当前时间。

The definition of week of year is locale dependent. 一年中一周的定义取决于地区。 See this related question for more information. 有关更多信息,请参阅此相关问题。 Why dec 31 2010 returns 1 as week of year? 为什么2010年12月31日将作为一年中的一周返回1? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM