2

年、週、曜日がわかれば、月と日を求めることができます。例えば

 // corresponding to September 15, 2012 if week starts on Monday
 int weekNum = 38;
 int dayNum = 6;
 int year = 2012;

 // set the calendar instance the a week of year and day in the future
  Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
 aGMTCalendar.setFirstDayOfWeek(Calendar.MONDAY);    
 aGMTCalendar.set(Calendar.WEEK_OF_YEAR,weekNum ); 
 aGMTCalendar.set(Calendar.DAY_OF_WEEK,dayNum );
 aGMTCalendar.set(Calendar.YEAR,year);

// get the month and day of month
 int   monthGMT = aGMTCalendar.get(Calendar.MONTH + 1); // returns 38  not 9

 int   dayOfMonthNumGMT = aGMTCalendar.get(Calendar.DAY_OF_MONTH); 
 // returns 14 but I wanted 15

ありがとうございました

4

4 に答える 4

1

これは

// +1 to the value of month returned, not to the value of MONTH constant.
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;  
于 2012-09-15T18:53:45.470 に答える
1

入手方法にmonthGMTは種類があります。そのはず:

int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;

各通話の後に以下の行を挿入すると、その行を呼び出した後、日付が 15 から 14 に変更されるaGMTCalendar.set()ことがわかります。 は を無視しますが、 を設定するときに考慮されます。dayNumaGMTCalendar.set(Calendar.DAY_OF_WEEK, dayNum)setFirstDayOfWeekWEEK_OF_YEAR

System.out.println(aGMTCalendar.getTime());
于 2012-09-15T18:59:11.003 に答える
0

リテラルCalendar.SATURDAYの代わりに定数を試してください。6

Calendar.SATURDAYあり7ません6

于 2012-09-15T19:12:35.903 に答える
0
 // corresponding to September 15, 2012 if week starts on Monday
 int weekNum = 38;
 int dayNum = 6;
 int year = 2012;

  // set the calendar instance the a week of year and day in the future
  Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
 aGMTCalendar.setFirstDayOfWeek(Calendar.MONDAY);    
 aGMTCalendar.set(Calendar.WEEK_OF_YEAR,weekNum ); 
 aGMTCalendar.set(Calendar.DAY_OF_WEEK,dayNum );
 aGMTCalendar.set(Calendar.YEAR,year);

 // get the month and day of month
 int   monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1; 
 // should be 10
 int   dayOfMonthNumGMT = aGMTCalendar.get(Calendar.DAY_OF_MONTH) + 1; 
 // should be 15
于 2012-09-15T19:14:36.683 に答える