我需要获得前一周的号码。例如。这是第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);
答案 0 :(得分: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);
答案 1 :(得分:3)
-1
会正常工作吗?
System.out.println("currentWeekNum = " + currentWeekNumber);
System.out.println("previousWeekNum = " + currentWeekNumber-1);
旁注:不要忘记检查当前周是否是第一周:)
由于上述解决方案不够模糊,而且正如其他人指出的那样(您也发现),请使用日历的添加方法,这样更准确。
cal.add(Calendar.WEEK_OF_YEAR, -1); //Then use this instance.
int preveWeekNum = cal.get(Calendar.WEEK_OF_YEAR);
答案 2 :(得分:2)
唯一正确的方法似乎是:
cal.add(Calendar.WEEK_OF_YEAR, -1);
int previousWeekNum = cal.get(Calendar.WEEK_OF_YEAR);
答案 3 :(得分:-1)
您正在获得当前周数。然后currentWeek -1将给出前一周。
int previousWeekNum = 0;
calender.add(Calendar.WEEK_OF_YEAR, -1);
previousWeekNum = calender.get(Calendar.WEEK_OF_YEAR);