這很難解釋,但我需要在給定日期後開始計算幾周。然後我必須得到當前的日期並查看它的週數。從給定日期開始的第幾周
已經嘗試了幾件事情,不知道這一點... 謝謝!
p.s.也許在兩個日期之間的天數差異併除以7?我將如何做到這一點?
這很難解釋,但我需要在給定日期後開始計算幾周。然後我必須得到當前的日期並查看它的週數。從給定日期開始的第幾周
已經嘗試了幾件事情,不知道這一點... 謝謝!
p.s.也許在兩個日期之間的天數差異併除以7?我將如何做到這一點?
我想你提到的獲得差異的方法將正常工作。您只需將給定日期設置爲一年中的某一天,然後使用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);
不知道如果我在最後數學是完全正確的,因爲我只是試圖想象在我的頭上,但不妨一試?希望這有助於。
的java.util.Calendar類有所有你需要做到這一點的方法:
Calendar c = Calendar.getInstance();
c.set(year, month, day);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
顯示使用你所嘗試過的。 – 2011-12-30 13:43:47
我已經嘗試了一些東西,例如查找給定日期的星期編號,然後從當前的星期編號中刪除它...我得到了一個fc。我是android編程的一個小菜鳥。仍在學習。目前試圖找出如何獲取兩個日期之間的日子,我想我可能已經找到了一些東西 – Lorof 2011-12-30 13:49:22