gpt4 book ai didi

java - 如何从给定的 WEEK_OF_YEAR、DAY_OF_WEEK、YEAR 获取 Calendar.MONTH

转载 作者:行者123 更新时间:2023-11-30 09:32:52 27 4
gpt4 key购买 nike

知道年、年中的星期和星期中的日期,就可以获得年中的月份和月份中的日期。例如

 // 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;

关于java - 如何从给定的 WEEK_OF_YEAR、DAY_OF_WEEK、YEAR 获取 Calendar.MONTH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12440112/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com