1. 程式人生 > >java 獲取上一週週一到週日的日期

java 獲取上一週週一到週日的日期

返回了map值

    public Map<String, Date> getLastWeek() {

        // TODO Auto-generated method stub
        Map<String, Date> map = new HashMap<String, Date>();
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        cal.add(Calendar.DATE, -(7 + (n - 1)));// 上週一的日期
        Date monday = cal.getTime();
        map.put("monday", monday);

        cal.add(Calendar.DATE, 1);
        Date tuesday = cal.getTime();
        map.put("tuesday", tuesday);

        cal.add(Calendar.DATE, 1);
        Date wednesday = cal.getTime();
        map.put("wednesday", wednesday);

        cal.add(Calendar.DATE, 1);
        Date thursday = cal.getTime();
        map.put("thursday", thursday);

        cal.add(Calendar.DATE, 1);
        Date friday = cal.getTime();
        map.put("friday", friday);

        cal.add(Calendar.DATE, 1);
        Date saturday = cal.getTime();
        map.put("saturday", saturday);

        cal.add(Calendar.DATE, 1);
        Date sunday = cal.getTime();
        map.put("sunday", sunday);
        return map;
    }