java 日期处理相关

/**
*获取指定日期 前后指定天数的 日期
*
*/
public static String getNewDate(String sdate, int days) throws Exception {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date inputDate = dateFormat.parse(sdate);

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(inputDate);
        calendar.add(Calendar.DAY_OF_MONTH, days);

        return dateFormat.format(calendar.getTime());
    }
@Test
    public void testdate() throws Exception {
        System.out.println(DateUtil.getNewDate("2011-07-11", -1));//2011-07-10
        System.out.println(DateUtil.getNewDate("2011-07-11", 1));//2011-07-12
    }

 

posted @ 2017-08-21 14:55  showme1942  阅读(94)  评论(0编辑  收藏  举报