从日历对象获取WEEK_OF_YEAR时出现问题 [英] I am getting issue while fetching WEEK_OF_YEAR from calendar object

查看:280
本文介绍了从日历对象获取WEEK_OF_YEAR时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从日历对象中获取 WEEK_OF_YEAR 时出现问题。

I am getting issue while fetching WEEK_OF_YEAR from calendar object.

by passing date '31-Dec-2014':
calendar1.get(Calendar.WEEK_OF_YEAR) - return 1  

by passing date '31-Dec-2015'  
calendar1.get(Calendar.WEEK_OF_YEAR) - return  53  

by passing date '31-Dec-2016'   
calendar1.get(Calendar.WEEK_OF_YEAR) - return 52

by passing date '31-Dec-2017'  
calendar1.get(Calendar.WEEK_OF_YEAR) - return 52  

by passing date '31-Dec-2018'   
calendar1.get(Calendar.WEEK_OF_YEAR) - return 1  

每次返回不同的值。您可以帮我解决这个问题。

Every time it returns different value . Could you please help me to solve this issue.

代码如下。

  Calendar calendar1 = Calendar.getInstance();
  calendar1.setTime('31-Dec-2012');
  calendar1.set(Calendar.HOUR_OF_DAY, 0);
  calendar1.setFirstDayOfWeek(Calendar.MONDAY);


推荐答案

WEEK_OF_YEAR 定义是Locale依赖,您可以在此javadoc

WEEK_OF_YEAR definition is Locale dependent as you can see in this javadoc :


为WEEK_OF_YEAR字段计算的值范围为1到53.日历的第一周year是从getFirstDayOfWeek()开始的最早的七天时间,其中至少包含该年的getMinimalDaysInFirstWeek()天。因此它取决于getMinimalDaysInFirstWeek(),getFirstDayOfWeek()和1月1日的星期几的值。一年的第1周和下一年的第1周(不包括)之间的周数从2到52顺序编号,或者53(除了涉及朱利安 - 格里高利转变的年份)。

Values calculated for the WEEK_OF_YEAR field range from 1 to 53. The first week of a calendar year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year (exclusive) are numbered sequentially from 2 to 52 or 53 (except for year(s) involved in the Julian-Gregorian transition).

还可以查看此问题或在 Wiki - ISO时钟查看周的差异。

also have a look at this question or at Wiki - ISO clock to see the week difference.

这篇关于从日历对象获取WEEK_OF_YEAR时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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