获取任意一周日期

项目需要,获取上一周周一和周日。
代码如下
// 如果需要制定日期转换的话
// String srcTime = "2010-08-11";
// Date temp = DateUtils.parseStringToUtilDate(srcTime);

		Calendar   calendar   =   Calendar.getInstance();
//calendar.setTime(temp);
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		calendar.add(Calendar.WEEK_OF_YEAR,-1); 
		System.out.println(DateUtils.formatDate(calendar.getTime()));
		
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		calendar.add(Calendar.WEEK_OF_YEAR,1); 
		System.out.println(DateUtils.formatDate(calendar.getTime()));


格式化日期代码
    public static String formatDate(Date myDate) {
        String strDate = null;

        if (myDate == null) {
            strDate = null;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        strDate = formatter.format(myDate);

        return strDate;
    }
    public static Date parseStringToUtilDate(String strDate) {
        Date result = null;

        try {
            SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
            result = myFmt.parse(strDate);
        } catch (Exception e) {
        }

        return result;
    }

你可能感兴趣的:(java)