比如说
今天是4-11号 要求出 周一也就是4-05号 和周日 4-11号这段代码是求出 以周日为一周开始的代码 求 以周一开始的 不知道改哪里
int dayofweek = cal.get(cal.DAY_OF_WEEK) - cal.getFirstDayOfWeek();   
    cal.add(cal.DATE,1 - dayofweek);             
    String monday = sdf.format(cal.getTime());
    System.out.println("本周一:"+monday);     

解决方案 »

  1.   

    楼主可以看一下这个,不知道有用吗
    http://www.javaeye.com/topic/407658
      

  2.   


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar c = Calendar.getInstance();
    int week1 = c.get(Calendar.DAY_OF_WEEK);
    c.add(Calendar.DAY_OF_MONTH,2-week1);
    System.out.println("1:  "+sdf.format(c.getTime()));
    c.add(Calendar.DAY_OF_MONTH,6);
    System.out.println("7:  "+sdf.format(c.getTime()));
      

  3.   

    public class Test {    public static void main(String[] args) {
            WeekScope scope = WeekScope.getInstance(WeekStart.MONDAY);
            System.out.println(scope);
        }
    }import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;public class WeekScope {    private WeekStart firstDayOfWeek;
        private Date firstDay;
        private Date lastDay;    private WeekScope(Date firstDay, Date lastDay, WeekStart firstDayOfWeek) {
            this.firstDay = firstDay;
            this.lastDay = lastDay;
            this.firstDayOfWeek = firstDayOfWeek;
        }    public static WeekScope getInstance(WeekStart firstDayOfWeek) {
            Calendar c = Calendar.getInstance();
            c.setFirstDayOfWeek(firstDayOfWeek.getValue());
            c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            Date firstDay = c.getTime();
            c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            Date lastDay = c.getTime();
            return new WeekScope(firstDay, lastDay, firstDayOfWeek);
        }    public Date getFirstDay() {
            return firstDay;
        }
        public Date getLastDay() {
            return lastDay;
        }    public String toString() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return "firstDayOfWeek: " + firstDayOfWeek + ", first: " + sdf.format(firstDay) + ", last: " + sdf.format(lastDay);
        }    public enum WeekStart {        SUNDAY(Calendar.SUNDAY),
            MONDAY(Calendar.MONDAY),
            TUESDAY(Calendar.TUESDAY),
            WEDNESDAY(Calendar.WEDNESDAY),
            THURSDAY(Calendar.THURSDAY),
            FRIDAY(Calendar.FRIDAY),
            SATURDAY(Calendar.SATURDAY);        private int value;        private WeekStart(int value) {
                this.value = value;
            }        public int getValue() {
                return this.value;
            }
        }
    }