-2

我需要得到前一周的号码。例如,这是第 38 周,我需要获得第 37 周。如何最好地进行?

int currentWeekNumber= now.get(Calendar.WEEK_OF_YEAR);
int previousWeekNum = // How to get the previous week number??

    System.out.println("currentWeekNum = " + currentWeekNumber);
    System.out.println(" previousWeekNum = " + previousWeekNum);
4

4 回答 4

5

这对我来说很好......这是获得它的正确语法

Calendar now = Calendar.getInstance();

    int currentWeek = now.get(Calendar.WEEK_OF_YEAR);
    System.out.println("current week = " + currentWeek);

now.add(Calendar.WEEK_OF_YEAR, -1);
    int test = now.get(Calendar.WEEK_OF_YEAR);
    System.out.println(" test date = " + test);
于 2013-09-20T09:38:21.717 回答
3

-1 会正常工作吗?

 System.out.println("currentWeekNum = " + currentWeekNumber);
 System.out.println("previousWeekNum = " + currentWeekNumber-1);

旁注:不要忘记检查当前周是否是第一周:)

由于上述解决方案有点模糊,并且正如其他人指出的那样(您也发现),请使用日历的 add 方法,哪个更准确。

cal.add(Calendar.WEEK_OF_YEAR, -1); //Then use this instance.
int preveWeekNum = cal.get(Calendar.WEEK_OF_YEAR);
于 2013-09-20T09:28:54.443 回答
2

唯一正确的方法似乎是:

cal.add(Calendar.WEEK_OF_YEAR, -1);
int previousWeekNum = cal.get(Calendar.WEEK_OF_YEAR);
于 2013-09-20T09:36:14.067 回答
-1

你有 currentWeek 计数。然后 currentWeek -1 将给出前一周。

int previousWeekNum = 0;
calender.add(Calendar.WEEK_OF_YEAR, -1);
previousWeekNum = calender.get(Calendar.WEEK_OF_YEAR);
于 2013-09-20T09:28:51.307 回答