比如我现在的时间2012-03-21,我想得到它这一周的星期四的日期,也就是2012-03-22,请问这我得怎么得到呢?

解决方案 »

  1.   

    public static String addDay(Date date , int num){
    //加上num天
    long time = date.getTime() + num*86400000;
    date.setTime(time);
    return  dateformat2.format(date);
    }
    private static SimpleDateFormat dateformat2 = new SimpleDateFormat("yyyy-MM-dd");
    很简单
      

  2.   

    用Calendar得到2012-03-21是星期x然后calendar.add(Calendar.DAY_OF_MONTH, 4-x);
      

  3.   

    我现在的问题是不知道他们之间相差多少,Num这个数我不知道。。
      

  4.   

    2楼+1        Calendar c=Calendar.getInstance();
            int now=c.get(Calendar.DAY_OF_WEEK);
    int temp=5;//周日是0,所以周四是5
    c.add(Calendar.DATE, temp-now);
    System.out.println(new SimpleDateFormat("yyyy-MM-dd").forma(c.getTime()));
      

  5.   


    周日到周六是1-7 周日是1 不是0 不过处理的时候你要注意你的周日是要怎么处理,你的周日是算一周的开始是是算一周的结束,Calendar中周日是算一周的开始的,如果你的周日是算一周的结束要判断下
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateFormat.parse("2012-03-18"));
    int week = calendar.get(Calendar.DAY_OF_WEEK);
    if(week == 1) {//周日
    calendar.add(Calendar.DAY_OF_MONTH, -3);
    } else {
    calendar.add(Calendar.DAY_OF_MONTH, 5 - week);
    }
    System.out.println(dateFormat.format(calendar.getTime()));
    System.out.println(calendar.get(Calendar.DAY_OF_WEEK));