加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java.util.Calendar WEEK_OF_YEAR为365和366天提供1

发布时间:2020-12-15 04:09:37 所属栏目:Java 来源:网络整理
导读:试图找到给定日期的周. 使用下面的代码. [ Java 1.7] 问题: 2012年12月29日=第52周 2012年12月30日=第1周 2013年12月30日=第52周 2013年12月31日=第1周 当年份= 365或366时,它将一年中的一周提供给1. 看起来有些模52发生了. 如何解决这个问题? import java
试图找到给定日期的周.
使用下面的代码. [ Java 1.7]

问题:
2012年12月29日=第52周
2012年12月30日=第1周
2013年12月30日=第52周
2013年12月31日=第1周

当年份= 365或366时,它将一年中的一周提供给1.
看起来有些模52发生了.
如何解决这个问题?

import java.util.Calendar;
import java.util.GregorianCalendar;

public class cal2{
   public static void main (String[] args) {

   Calendar mycal = GregorianCalendar.getInstance();  

   mycal.setLenient(false);

   int year  = 2012;
   int month = 11;  //0=Jan,11=Dec
   int date  = 29; 

   mycal.set(year,1);
   mycal.setFirstDayOfWeek(mycal.get(mycal.DAY_OF_WEEK));

   mycal.set(year,month,date);
   System.out.println("n>>>>>>>>WEEK :"+mycal.get(mycal.WEEK_OF_YEAR));

   // System.out.println("nDATE :"+mycal);

   }

}

解决方法

来自Docs的 SEE HERE

第一周

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.

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读