我想在下周五甚至一周(考虑一年中的周数从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
答案 0 :(得分: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()));