日期:2014-05-20  浏览次数:20811 次

怎样计算出与当前给出日期后面的最近的星期六?
如果我给的日期是2007-02-28,是星期四,那么星期六是2007-03-03,我怎么才能计算出来,能否给出一个完整的程序。

我现在用calendar.get(Calendar.DAY_OF_WEEK))可以得出2007-02-28时星期几,然后用calendar.add(Calendar.DAY_OF_MONTH,1);可得出的是2007-02-29,这个月没有29号,怎么判断跨月时的情况,是否还要判断闰年闰月?

------解决方案--------------------
import java.util.*;
import java.text.*;
public class Test {
public static void main(String[] args) throws ParseException{
String strDate = "2007-02-28 ";
DateFormat df = new SimpleDateFormat( "yyyy-MM-dd ");
Calendar c = Calendar.getInstance();
c.setTime(df.parse(strDate));
c.add(Calendar.DAY_OF_MONTH, Calendar.SATURDAY-c.get(Calendar.DAY_OF_WEEK));
System.out.println(df.format(c.getTime()));
}
}
------解决方案--------------------
public class Test
{
public static void main(String[] args)
{
java.util.Calendar c=java.util.Calendar.getInstance();
java.text.DateFormat df=new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss ");
System.out.println( "当前时间: "+df.format(c.getTime()));
c.set(java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.SATURDAY);
System.out.println( "最近周六时间: "+df.format(c.getTime()));
}
}

------解决方案--------------------
public class Test {

public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(2007, 1, 28); // 2007-2-28
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
System.out.println(cal.getTime());
}
}