2

我想得到偶数周的下一个星期五(考虑到一年中从 1 到 54 的周数)。

例如,如果今天是星期五,而这周是偶数,我想要下一个星期五。如果今天是奇数周的星期四,我想要明天的日期。

测试输入/输出如下:

 14/09/2012 -> 14/09/2012
 15/09/2012 -> 28/09/2012
 21/09/2012 -> 28/09/2012
 22/09/2012 -> 28/09/2012
 29/09/2012 -> 12/10/2012
 25/12/2012 -> 04/01/2013
 29/12/2012 -> 04/01/2013
4

1 回答 1

4
Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
int daysUntilNextFriday = Calendar.FRIDAY - dayOfWeek;
if(daysUntilNextFriday < 0){
    daysUntilNextFriday = daysUntilNextFriday + 7;
}
Calendar nextFriday = (Calendar)today.clone();
nextFriday.add(Calendar.DAY_OF_WEEK, daysUntilNextFriday);
if(nextFriday.get(Calendar.WEEK_OF_YEAR) % 2 == 0){
    nextFriday.add(Calendar.DAY_OF_WEEK, 7);
}
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(nextFriday.getTime()));
于 2012-09-19T17:59:39.797 回答