这有点难以解释,但我需要在给定日期之后数周开始计算。然后我必须得到当前日期并检查它是什么周数。
已经尝试了一些事情,但无法弄清楚这一点...... 谢谢!
P.S。也许在两个日期之间得到差异并除以7?我该怎么做?
答案 0 :(得分:2)
我认为你提到的获得差异的方法可以正常工作。您只需将给定日期设置为一年中的某一天,然后使用Calendar类获取一年中的当前日期。
//Set Given Date to what you want it to be, eg 10th March 2006. (Months are indexed from 0-11)
Calendar calendarGivenDate = Calendar.getInstance();
calendarGivenDate.set(Calendar.DAY_OF_MONTH, 10);
calendarGivenDate.set(Calendar.MONTH_OF_YEAR, 2);
calendarGivenDate.set(Calendar.YEAR, 2006);
//Receive the day of the year for what you previously set
int givenDateDayOfYear = calendarGivenDate.get(Calendar.DAY_OF_YEAR);
//Receive current day of year
Calendar calendarCurrentDate = Calendar.getInstance();
int currentDateDayOfYear = calendarCurrentDate.get(Calendar.DAY_OF_YEAR);
//Get difference in number of years
int currentYear = calendarCurrentDate.get(Calendar.YEAR);
int givenYear = calendarGivenDate.get(Calendar.YEAR);
int yearDifference = currentYear - givenYear;
//Find difference, divide by 7 (Round value down to get the difference in whole weeks)
double differenceDays = currentDateDayOfYear - givenDateDayOfYear + (365*yearDifference);
double differenceWeeks = Math.floor(differenceDays / 7);
不确定我的数学结果是否完全正确,因为我只是想在脑海中想象它,但试一试?希望这有帮助。
答案 1 :(得分:0)
java.util.Calendar类具有执行此操作所需的所有方法:
Calendar c = Calendar.getInstance();
c.set(year, month, day);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);