java获取字符串格式日期向前或向后n天的日期

时间:2024-09-02 09:36:02
private void setTilteMessage(){ 
        BaseDao dao = new BaseDao(); 
        String titleData = dao.getLatestData(); 
        dao.closeDB(); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        try
            Date date = sdf.parse(titleData); 
            Calendar   calendar   =   new   GregorianCalendar();  
            calendar.setTime(date);  
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动  
            date=calendar.getTime();   //这个时间就是日期往后推一天的结果  
            String putDate = sdf.format(date); //增加一天后的日期 
               
        } catch (ParseException e) { 
            e.printStackTrace(); 
        
    }