Java 如何从给定的一年中的一周、一周中的一天、一年中的一周中获取Calendar.MONTH
知道年、年中的周和周中的日可以获得年中的月和月中的日。比如说Java 如何从给定的一年中的一周、一周中的一天、一年中的一周中获取Calendar.MONTH,java,calendar,Java,Calendar,知道年、年中的周和周中的日可以获得年中的月和月中的日。比如说 // 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 = Ca
// 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
谢谢这应该是
// +1 to the value of month returned, not to the value of MONTH constant.
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
获取
monthGMT
的方式有一个类型。应该是:
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
在每次调用aGMTCalendar.set()
后,将下面的行放在调用dayNum
one后,日期从15变为14。aGMTCalendar.set(Calendar.DAY\u OF_WEEK,dayNum)
忽略setFirstDayOfWeek
,但是在设置周OF_YEAR
时会考虑该设置
System.out.println(aGMTCalendar.getTime());
尝试
Calendar.SATURDAY
constant而不是6
literal
日历。周六实际上是7
而不是6
谢谢您的回复。虽然我试图通过aGMTCalendar将一周的第一天设置为星期一(星期一应为1)。setFirstDayOfWeek(Calendar.Monday)当我输入dayOfWeekNum weekNum=6 38时,我得到了month/day=9/14再次感谢您的帮助。。。我还在挣扎
// 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