2

WEEK_OF_YEARカレンダー オブジェクトから取得中に問題が発生しました。

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  

毎回異なる値を返します。この問題を解決するために私を助けてください。

コードは以下の通りです。

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

2 に答える 2

2

WEEK_OF_YEARこの javadoc でわかるように、定義はロケールに依存します。

WEEK_OF_YEAR フィールドの計算値の範囲は 1 ~ 53 です。暦年の最初の週は、その年の getMinimalDaysInFirstWeek() 日以上を含む getFirstDayOfWeek() から始まる最も早い 7 日間です。したがって、getMinimalDaysInFirstWeek()、getFirstDayOfWeek() の値、および 1 月 1 日の曜日に依存します。ある年の第 1 週と翌年の第 1 週の間 (除外) の週には、2 から 52 までの連続した番号が付けられます。 53 (ユリウス・グレゴリオ暦移行に関与した年を除く)。

this questionまたはWiki - ISO clockも見て、週の違いを確認してください。

于 2012-08-20T12:43:42.780 に答える
1

それはだまされた質問ですが、簡単に言えば

Calendar c = Calendar.getInstance();
c.setMinimalDaysInFirstWeek(7);//anything more than 1 will work in this year
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime( sdf.parse("31/12/2010"));
System.out.println( c.get( Calendar.WEEK_OF_YEAR ) );

2012 年 12 月 31 日の場合は 53 を返します

于 2012-08-20T13:53:07.697 に答える