了解java.util.Calendar WEEK_OF_YEAR [英] Understanding java.util.Calendar WEEK_OF_YEAR

查看:165
本文介绍了了解java.util.Calendar WEEK_OF_YEAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 java.util.Calendar.get(java.util.Calendar.WEEK_OF_YEAR)如何工作,但似乎我缺少一些

I'm trying to understand how java.util.Calendar.get(java.util.Calendar.WEEK_OF_YEAR) works, but it seems that I'm missing some points.

String time = "1998-12-31"; // year month day
java.util.Calendar date = java.util.Calendar.getInstance();
date.setTime((new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(time));
System.err.println("Week of year = " + date.get(java.util.Calendar.WEEK_OF_YEAR));
// Week of year = 1 Why ???

为什么 date.get(java.util.Calendar.WEEK_OF_YEAR) code>每年的最后一周返回1?

Why date.get(java.util.Calendar.WEEK_OF_YEAR) returns 1 for the last week of the year?

此外, WEEK_OF_YEAR $ c>1998-01-01为1,1998-12-23为52.

有人对此行为有解释吗?

Moreover, WEEK_OF_YEAR for "1998-01-01" is 1 and for "1998-12-23" it is 52.
Does anybody have an explanation for this behavior?

推荐答案

java.util.Calendar javadoc


第一周



日历使用两个
参数定义一个特定于区域设置的七天周:一周的第一天和最早的天数
周(从1到7)。构建日历时,这些数字取自语言环境资源
数据。

First Week

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

设置或获取WEEK_OF_MONTH或WEEK_OF_YEAR字段时,
日历必须确定月或年的第一周作为
参考点。一个月或一年的第一周被定义为从getFirstDayOfWeek()开始的
最早的七天时间,以及包含该月的至少getMinimalDaysInFirstWeek()天或
年的
。星期编号为...,-1,0在第一周之前;周编号
2,3,...跟着它。注意,由
get()返回的标准化编号可能不同。例如,特定日历子类可以将
指定一年中第一周之前的一周作为先前
年的第n周。

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 a reference point. The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year. Weeks numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow it. Note that the normalized numbering returned by get() may be different. For example, a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.

所以它是locale特定的。

So it's locale-specific. In your case, if the week contains days from new year, it is counted as week 1 from the new year.

您可以使用

You can change this behavior by using Calendar#setMinimalDaysInFirstWeek(int).

这篇关于了解java.util.Calendar WEEK_OF_YEAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆